Skip to content

Commit

Permalink
fix: Custom Report Total Mode not showing offbudget transactions #3627
Browse files Browse the repository at this point in the history
  • Loading branch information
UnderKoen committed Oct 11, 2024
1 parent f9c08a9 commit e135d55
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 61 deletions.
42 changes: 16 additions & 26 deletions packages/desktop-client/src/components/reports/ReportOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,62 +232,47 @@ export type QueryDataEntity = {
amount: number;
};

type UncategorizedId = 'off_budget' | 'transfer' | 'other' | 'all';

export type UncategorizedEntity = Pick<
CategoryEntity,
'id' | 'name' | 'hidden'
'id' | 'name' | 'hidden' | 'cat_group'
> & {
/*
When looking at uncategorized and hidden transactions we
need a way to group them. To do this we give them a unique
uncategorized_id. We also need a way to filter the
transctions from our query. For this we use the 3 variables
below.
*/
uncategorized_id?: string;
is_off_budget?: boolean;
is_transfer?: boolean;
has_category?: boolean;
uncategorized_id?: UncategorizedId;
};

const uncategorizedCategory: UncategorizedEntity = {
id: '',
name: 'Uncategorized',
uncategorized_id: '1',
uncategorized_id: 'other',
hidden: false,
is_off_budget: false,
is_transfer: false,
has_category: false,
};
const transferCategory: UncategorizedEntity = {
id: '',
name: 'Transfers',
uncategorized_id: '2',
uncategorized_id: 'transfer',
hidden: false,
is_off_budget: false,
is_transfer: true,
has_category: false,
};
const offBudgetCategory: UncategorizedEntity = {
id: '',
name: 'Off Budget',
uncategorized_id: '3',
uncategorized_id: 'off_budget',
hidden: false,
is_off_budget: true,
is_transfer: false,
has_category: true,
};

type UncategorizedGroupEntity = Pick<
CategoryGroupEntity,
'name' | 'id' | 'hidden'
> & {
categories?: UncategorizedEntity[];
uncategorized_id?: UncategorizedId;
};

const uncategorizedGroup: UncategorizedGroupEntity = {
name: 'Uncategorized & Off Budget',
id: 'uncategorized',
hidden: false,
uncategorized_id: 'all',
categories: [uncategorizedCategory, transferCategory, offBudgetCategory],
};

Expand All @@ -302,7 +287,7 @@ export const categoryLists = (categories: {
const catGroupB = categories.grouped.find(f => f.id === b.cat_group);
//initial check that both a and b have a sort_order and category group
return a.sort_order && b.sort_order && catGroupA && catGroupB
? /*sorting by "is_income" because sort_order for this group is
? /*sorting by "is_income" because sort_order for this group is
separate from other groups*/
Number(catGroupA.is_income) - Number(catGroupB.is_income) ||
//Next, sorting by group sort_order
Expand Down Expand Up @@ -342,7 +327,12 @@ export const groupBySelections = (
break;
case 'Group':
groupByList = categoryGroup.map(group => {
return { id: group.id, name: group.name, hidden: group.hidden };
return {
...group,
id: group.id,
name: group.name,
hidden: group.hidden,
};
});
groupByLabel = 'categoryGroup';
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ export function createCustomSpreadsheet({
let netAssets = 0;
let netDebts = 0;

const groupsByCategory =
groupByLabel === 'category' || groupByLabel === 'categoryGroup';
const intervalData = intervals.reduce(
(arr: IntervalEntity[], intervalItem, index) => {
let perIntervalAssets = 0;
Expand All @@ -163,11 +165,13 @@ export function createCustomSpreadsheet({
showOffBudget,
showHiddenCategories,
showUncategorized,
groupsByCategory,
)
.filter(
asset =>
asset.date === intervalItem &&
asset[groupByLabel] === (item.id ?? null),
(asset[groupByLabel] === (item.id ?? null) ||
(item.uncategorized_id && groupsByCategory)),
)
.reduce((a, v) => (a = a + v.amount), 0);
perIntervalAssets += intervalAssets;
Expand All @@ -178,11 +182,13 @@ export function createCustomSpreadsheet({
showOffBudget,
showHiddenCategories,
showUncategorized,
groupsByCategory,
)
.filter(
debt =>
debt.date === intervalItem &&
debt[groupByLabel] === (item.id ?? null),
(debt[groupByLabel] === (item.id ?? null) ||
(item.uncategorized_id && groupsByCategory)),
)
.reduce((a, v) => (a = a + v.amount), 0);
perIntervalDebts += intervalDebts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,40 @@ export function filterHiddenItems(
showOffBudget?: boolean,
showHiddenCategories?: boolean,
showUncategorized?: boolean,
groupByCategory?: boolean,
) {
const showHide = data
.filter(e =>
!showHiddenCategories
? e.categoryHidden === false && e.categoryGroupHidden === false
: true,
.filter(
e =>
showHiddenCategories ||
(e.categoryHidden === false && e.categoryGroupHidden === false),
)
.filter(f =>
showOffBudget
? showUncategorized
? //true,true
true
: //true,false
f.category !== null ||
f.accountOffBudget !== false ||
f.transferAccount !== null
: showUncategorized
? //false, true
f.accountOffBudget === false &&
(f.transferAccount === null || f.category !== null)
: //false false
f.category !== null && f.accountOffBudget === false,
);
.filter(e => showOffBudget || e.accountOffBudget === false)
.filter(e => showUncategorized || e.category !== null);

return showHide.filter(query => {
if (!item.uncategorized_id) {
return true;
}
if (!groupByCategory) return true;

const hasCategory = !!query.category;
const isOffBudget = query.accountOffBudget;
const isTransfer = !!query.transferAccount;

const isTransfer = item.is_transfer
? query.transferAccount
: !query.transferAccount;
const isHidden = item.has_category ? true : !query.category;
const isOffBudget = item.is_off_budget
? query.accountOffBudget
: !query.accountOffBudget;
if (hasCategory && !isOffBudget) {
return item.uncategorized_id == null;
}

return isTransfer && isHidden && isOffBudget;
switch (item.uncategorized_id) {
case 'off_budget':
return isOffBudget;
case 'transfer':
return isTransfer && !isOffBudget;
case 'other':
return !isOffBudget && !isTransfer;
case 'all':
console.log('all');
return true;
default:
return false;
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,21 @@ export function recalculate({
(arr: IntervalEntity[], intervalItem, index) => {
const last = arr.length === 0 ? null : arr[arr.length - 1];

const groupsByCategory =
groupByLabel === 'category' || groupByLabel === 'categoryGroup';
const intervalAssets = filterHiddenItems(
item,
assets,
showOffBudget,
showHiddenCategories,
showUncategorized,
groupsByCategory,
)
.filter(
asset =>
asset.date === intervalItem &&
asset[groupByLabel] === (item.id ?? null),
(asset[groupByLabel] === (item.id ?? null) ||
(item.uncategorized_id && groupsByCategory)),
)
.reduce((a, v) => (a = a + v.amount), 0);
totalAssets += intervalAssets;
Expand All @@ -64,11 +68,13 @@ export function recalculate({
showOffBudget,
showHiddenCategories,
showUncategorized,
groupsByCategory,
)
.filter(
debt =>
debt.date === intervalItem &&
debt[groupByLabel] === (item.id ?? null),
(debt[groupByLabel] === (item.id ?? null) ||
(item.uncategorized_id && groupsByCategory)),
)
.reduce((a, v) => (a = a + v.amount), 0);
totalDebts += intervalDebts;
Expand Down Expand Up @@ -98,6 +104,8 @@ export function recalculate({
[],
);

console.log(item, intervalData);

const totalTotals = totalAssets + totalDebts;

return {
Expand Down

0 comments on commit e135d55

Please sign in to comment.