Skip to content

Commit

Permalink
Fix negative asset values (#866)
Browse files Browse the repository at this point in the history
  • Loading branch information
argaen authored May 20, 2024
1 parent 33148c6 commit 7298942
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/components/charts/TotalsPie.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe('TotalsPie', () => {
datasets: [
{
backgroundColor: ['#111', '#222'],
data: [1500, 150],
data: [1500, -150],
},
],
labels: ['Assets', 'Liabilities'],
Expand Down
3 changes: 1 addition & 2 deletions src/__tests__/components/pages/account/TotalWidget.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ describe('TotalWidgetTest', () => {
);
});

it('keeps negative for liability', () => {
account.type = 'CREDIT';
it('keeps negative values', () => {
jest.spyOn(apiHook, 'useAccountsTotals').mockReturnValue({
data: { guid: new Money(-100, 'EUR') } as AccountsTotals,
});
Expand Down
22 changes: 12 additions & 10 deletions src/__tests__/components/tables/AccountsTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,10 @@ describe('AccountsTable', () => {
expect(container).toMatchSnapshot();
});

it('renders Total column as expected', async () => {
it.each([
['ASSET', new Money(10, 'EUR'), '€10.00'],
['ASSET', new Money(-10, 'EUR'), '-€10.00'],
])('renders Total column for %s with correct symbol', async (type, money, expected) => {
render(<AccountsTable guids={['a1']} />);

await screen.findByTestId('Table');
Expand All @@ -333,29 +336,28 @@ describe('AccountsTable', () => {

expect(
// @ts-ignore
totalCol.accessorFn({ total: new Money(10, 'EUR') }),
).toEqual(10);
totalCol.accessorFn({ total: money }),
).toEqual(money.toNumber());

expect(totalCol.cell).not.toBeUndefined();
const { container } = render(
render(
// @ts-ignore
totalCol.cell({
row: {
original: {
account: {
guid: 'assets',
name: 'Assets',
type: 'ASSET',
guid: 'account',
name: 'Account',
type,
childrenIds: ['a1'],
} as Account,
total: new Money(10, 'EUR'),
total: money,
children: [],
},
},
}),
);

await screen.findByText('€10.00');
expect(container).toMatchSnapshot();
await screen.findByText(expected);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,3 @@ exports[`AccountsTable renders Name column as expected when expandable and not e
</div>
</div>
`;

exports[`AccountsTable renders Total column as expected 1`] = `
<div>
<span>
€10.00
</span>
</div>
`;
47 changes: 40 additions & 7 deletions src/__tests__/lib/queries/getAccountsTotals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('getAccountsTotals', () => {
let root: Account;
let assetAccount: Account;
let expensesAccount: Account;
let incomeAccount: Account;

beforeEach(async () => {
datasource = new DataSource({
Expand All @@ -39,7 +40,7 @@ describe('getAccountsTotals', () => {
}).save();

assetAccount = await Account.create({
guid: 'abcdef',
guid: 'guid1',
name: 'Assets',
type: 'ASSET',
fk_commodity: eur,
Expand All @@ -48,14 +49,23 @@ describe('getAccountsTotals', () => {
await assetAccount.reload();

expensesAccount = await Account.create({
guid: 'ghijk',
guid: 'guid2',
name: 'Expenses',
type: 'EXPENSE',
fk_commodity: eur,
parent: root,
}).save();
await expensesAccount.reload();

incomeAccount = await Account.create({
guid: 'guid3',
name: 'Income',
type: 'INCOME',
fk_commodity: eur,
parent: root,
}).save();
await incomeAccount.reload();

await root.reload();
});

Expand Down Expand Up @@ -121,13 +131,36 @@ describe('getAccountsTotals', () => {
],
}).save();

await Transaction.create({
description: 'description',
date: DateTime.fromISO('2022-08-01'),
fk_currency: eur,
splits: [
Split.create({
valueNum: -75,
valueDenom: 1,
quantityNum: -75,
quantityDenom: 1,
fk_account: incomeAccount,
}),
Split.create({
valueNum: 75,
valueDenom: 1,
quantityNum: 75,
quantityDenom: 1,
fk_account: assetAccount,
}),
],
}).save();

const totals = await getAccountsTotals(
[root, assetAccount, expensesAccount],
[root, assetAccount, expensesAccount, incomeAccount],
TEST_INTERVAL,
);

expect(totals.abcdef.toString()).toEqual('-300 EUR');
expect(totals.ghijk.toString()).toEqual('300 EUR');
expect(totals.guid1.toString()).toEqual('-225 EUR');
expect(totals.guid2.toString()).toEqual('300 EUR');
expect(totals.guid3.toString()).toEqual('75 EUR');
});

it('filters by date', async () => {
Expand Down Expand Up @@ -179,7 +212,7 @@ describe('getAccountsTotals', () => {
TEST_INTERVAL,
);

expect(totals.abcdef.toString()).toEqual('-100 EUR');
expect(totals.ghijk.toString()).toEqual('100 EUR');
expect(totals.guid1.toString()).toEqual('-100 EUR');
expect(totals.guid2.toString()).toEqual('100 EUR');
});
});
4 changes: 2 additions & 2 deletions src/components/charts/TotalsPie.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default function TotalsPie({
datasets: [
{
backgroundColor,
data: data.map(d => Math.abs(d.toNumber())),
data: data.map(d => d.toNumber()),
},
],
}}
Expand Down Expand Up @@ -100,7 +100,7 @@ export default function TotalsPie({
<div className="-mt-12">
<p className="flex justify-center">{title}</p>
<p className="flex justify-center text-xl">
{total.abs().format()}
{total.format()}
</p>
</div>
</>
Expand Down
3 changes: 1 addition & 2 deletions src/components/pages/account/TotalWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import Money from '@/book/Money';
import type { Account } from '@/book/entities';
import StatisticsWidget from '@/components/StatisticsWidget';
import TotalChange from '@/components/widgets/TotalChange';
import { isLiability } from '@/book/helpers';

export type TotalWidgetProps = {
account: Account,
Expand All @@ -21,7 +20,7 @@ export default function TotalWidget({
<StatisticsWidget
className="mr-2"
title="Total"
stats={isLiability(account) ? total0.format() : total0.abs().format()}
stats={total0.format()}
description={<TotalChange account={account} />}
/>
);
Expand Down
4 changes: 2 additions & 2 deletions src/components/tables/AccountsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ const columns: ColumnDef<AccountsTableRow>[] = [
),
},
{
accessorFn: (row: AccountsTableRow) => row.total.abs().toNumber(),
accessorFn: (row: AccountsTableRow) => row.total.toNumber(),
id: 'total',
header: '',
cell: ({ row }) => (
<span>
{row.original.total.abs().format()}
{row.original.total.format()}
</span>
),
},
Expand Down
5 changes: 4 additions & 1 deletion src/lib/queries/getAccountsTotals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ export default async function getAccountsTotals(
const totals: { [guid: string]: Money } = {};

rows.forEach(row => {
totals[row.accountId] = new Money(row.total, accountsMap[row.accountId].commodity.mnemonic);
totals[row.accountId] = new Money(
accountsMap[row.accountId].type === 'INCOME' ? Math.abs(row.total) : row.total,
accountsMap[row.accountId].commodity.mnemonic,
);
});

return totals;
Expand Down

0 comments on commit 7298942

Please sign in to comment.