Skip to content

Commit

Permalink
Improve sum calculation for archives when search filter is applied
Browse files Browse the repository at this point in the history
  • Loading branch information
mtotschnig committed Feb 1, 2025
1 parent 406eb8a commit cc0baee
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,8 @@ abstract class BaseTransactionProvider : ContentProvider() {
}.toTypedArray()

val sql = buildTransactionGroupCte(
listOfNotNull(accountQuery, selection).joinToString(" AND "),
accountQuery,
selection,
forHome,
typeWithFallBack
) + " " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,8 @@ private static void ensureLocalized() {
KEY_CR_STATUS + " != '" + CrStatus.VOID.name() + "'";
public static final String WHERE_NOT_ARCHIVED =
KEY_STATUS + " != " + STATUS_ARCHIVED;
public static final String WHERE_NOT_ARCHIVE =
KEY_STATUS + " != " + STATUS_ARCHIVE;

public static final String WHERE_DEPENDENT = KEY_PARENTID + " = ? OR " + KEY_ROWID + " IN "
+ "(SELECT " + KEY_TRANSFER_PEER + " FROM " + TABLE_TRANSACTIONS + " WHERE " + KEY_PARENTID + "= ?)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import org.totschnig.myexpenses.provider.DatabaseConstants.TABLE_TRANSACTIONS
import org.totschnig.myexpenses.provider.DatabaseConstants.TABLE_TRANSACTIONS_TAGS
import org.totschnig.myexpenses.provider.DatabaseConstants.TREE_CATEGORIES
import org.totschnig.myexpenses.provider.DatabaseConstants.VIEW_WITH_ACCOUNT
import org.totschnig.myexpenses.provider.DatabaseConstants.WHERE_NOT_ARCHIVE
import org.totschnig.myexpenses.provider.DatabaseConstants.WHERE_NOT_ARCHIVED
import org.totschnig.myexpenses.provider.DatabaseConstants.WHERE_NOT_SPLIT
import org.totschnig.myexpenses.provider.DatabaseConstants.WHERE_NOT_VOID
Expand Down Expand Up @@ -650,10 +651,15 @@ fun buildSearchCte(


fun buildTransactionGroupCte(
selection: String,
accountQuery: String,
selection: String?,
forHome: String?,
typeWithFallBack: String
): String {
// If a filter is applied to the transaction list, we need to calculate with the contents
// of the archive otherwise we just take the archive itself into account
val withFilter = selection != null
val selection = listOfNotNull(accountQuery, selection).joinToString(" AND ")
return buildString {
append("WITH $CTE_TRANSACTION_GROUPS AS (SELECT ")
append(KEY_DATE)
Expand All @@ -665,7 +671,7 @@ fun buildTransactionGroupCte(
append(getAmountCalculation(forHome, VIEW_WITH_ACCOUNT))
append(" END AS integer) AS $KEY_DISPLAY_AMOUNT")
append(" FROM $VIEW_WITH_ACCOUNT")
append(" WHERE $WHERE_NOT_SPLIT AND $WHERE_NOT_ARCHIVED AND $selection)")
append(" WHERE $WHERE_NOT_SPLIT AND ${if (withFilter) WHERE_NOT_ARCHIVE else WHERE_NOT_ARCHIVED} AND $selection)")
}
}

Expand Down Expand Up @@ -722,13 +728,6 @@ AND ($WHERE_NOT_SPLIT AND $WHERE_NOT_ARCHIVED AND $WHERE_NOT_VOID AND $selection
}
}

//when either both sides are homeCurrency, or both sides are foreign currency, we select based on amount
//otherwise we only select the part from the homeCurrency
fun grandTotalAccountKeepTransferPartCriterion(homeCurrency: String) = """
CASE WHEN ($KEY_CURRENCY = '$homeCurrency') = ((SELECT $KEY_CURRENCY FROM $TABLE_ACCOUNTS WHERE $KEY_ROWID = $KEY_TRANSFER_ACCOUNT) = '$homeCurrency')
THEN $KEY_AMOUNT < 0 ELSE $KEY_CURRENCY = '$homeCurrency' END
"""

fun archiveSumCTE(
archiveId: Long,
typeWithFallBack: String
Expand Down

0 comments on commit cc0baee

Please sign in to comment.