2
0
mirror of https://github.com/frappe/books.git synced 2024-11-08 06:44:06 +00:00

refactor: update CoA balance calculation

- update tests
This commit is contained in:
18alantom 2022-11-07 13:22:43 +05:30 committed by Alan
parent d73d331a87
commit 09e2da3abd
8 changed files with 66 additions and 17 deletions

View File

@ -119,4 +119,15 @@ export class BespokeQueries {
return { income, expense };
}
static async getTotalCreditAndDebit(db: DatabaseCore) {
return await db.knex!.raw(`
select
account,
sum(cast(credit as real)) as totalCredit,
sum(cast(debit as real)) as totalDebit
from AccountingLedgerEntry
group by account
`);
}
}

View File

@ -1,11 +1,12 @@
import fs from 'fs/promises';
import { DatabaseError } from 'fyo/utils/errors';
import path from 'path';
import { DatabaseDemuxBase, DatabaseMethod } from 'utils/db/types';
import { getSchemas } from '../../schemas';
import {
databaseMethodSet,
emitMainProcessError,
unlinkIfExists,
unlinkIfExists
} from '../helpers';
import patches from '../patches';
import { BespokeQueries } from './bespoke';
@ -148,7 +149,7 @@ export class DatabaseManager extends DatabaseDemuxBase {
}
if (!BespokeQueries.hasOwnProperty(method)) {
return;
throw new DatabaseError(`invalid bespoke db function ${method}`);
}
// @ts-ignore

View File

@ -14,7 +14,7 @@ import {
DatabaseDemuxConstructor,
DocValue,
DocValueMap,
RawValueMap,
RawValueMap
} from './types';
// Return types of Bespoke Queries
@ -23,6 +23,11 @@ type TotalOutstanding = { total: number; outstanding: number };
type Cashflow = { inflow: number; outflow: number; yearmonth: string }[];
type Balance = { balance: number; yearmonth: string }[];
type IncomeExpense = { income: Balance; expense: Balance };
type TotalCreditAndDebit = {
account: string;
totalCredit: number;
totalDebit: number;
};
export class DatabaseHandler extends DatabaseBase {
#fyo: Fyo;
@ -280,6 +285,12 @@ export class DatabaseHandler extends DatabaseBase {
)) as IncomeExpense;
}
async getTotalCreditAndDebit(): Promise<unknown> {
return (await this.#demux.callBespoke(
'getTotalCreditAndDebit'
)) as TotalCreditAndDebit[];
}
/**
* Internal methods
*/

View File

@ -11,7 +11,6 @@ export class AccountingLedgerEntry extends Doc {
credit?: Money;
referenceType?: string;
referenceName?: string;
balance?: Money;
reverted?: boolean;
async revert() {
@ -46,7 +45,6 @@ export class AccountingLedgerEntry extends Doc {
'party',
'debit',
'credit',
'balance',
'referenceName',
'reverted',
],

View File

@ -137,11 +137,5 @@
"default": false
}
],
"quickEditFields": [
"rootType",
"parentAccount",
"accountType",
"isGroup",
"balance"
]
"quickEditFields": ["rootType", "parentAccount", "accountType", "isGroup"]
}

View File

@ -26,7 +26,7 @@ test('Meta Properties', function (t) {
});
test('Field Counts', function (t) {
t.equal(appSchemaMap.Account.fields?.length, 6);
t.equal(appSchemaMap.Account.fields?.length, 5);
t.equal(appSchemaMap.JournalEntry.fields?.length, 9);
t.equal(appSchemaMap.JournalEntryAccount.fields?.length, 3);
t.equal(appSchemaMap.Party.fields?.length, 9);

View File

@ -3,7 +3,10 @@
<PageHeader :title="t`Chart of Accounts`" />
<!-- Chart of Accounts -->
<div class="flex-1 flex flex-col overflow-y-auto mb-4 custom-scroll" v-if="root">
<div
class="flex-1 flex flex-col overflow-y-auto mb-4 custom-scroll"
v-if="root"
>
<!-- Chart of Accounts Indented List -->
<template v-for="account in allAccounts" :key="account.name">
<!-- Account List Item -->
@ -142,6 +145,7 @@ import PageHeader from 'src/components/PageHeader';
import { fyo } from 'src/initFyo';
import { docsPathMap } from 'src/utils/misc';
import { docsPath, openQuickEdit } from 'src/utils/ui';
import { getMapFromList } from 'utils/index';
import { nextTick } from 'vue';
import { handleErrorWithDialog } from '../errorHandling';
@ -156,22 +160,54 @@ export default {
schemaName: 'Account',
newAccountName: '',
insertingAccount: false,
totals: {},
refetchTotals: false,
};
},
async mounted() {
await this.setTotalDebitAndCredit();
fyo.doc.observer.on('sync:AccountingLedgerEntry', () => {
this.refetchTotals = true;
});
},
async activated() {
this.fetchAccounts();
if (fyo.store.isDevelopment) {
window.coa = this;
}
docsPath.value = docsPathMap.ChartOfAccounts;
if (this.refetchTotals) {
await this.setTotalDebitAndCredit();
this.refetchTotals = false;
}
},
deactivated() {
docsPath.value = '';
},
methods: {
getBalance(account) {
const total = this.totals[account.name];
if (!total) {
return 0;
}
const { totalCredit, totalDebit } = total;
if (isCredit(account.rootType)) {
return totalCredit - totalDebit;
}
return totalDebit - totalCredit;
},
getBalanceString(account) {
const suffix = isCredit(account.rootType) ? t`Cr.` : t`Dr.`;
return `${fyo.format(account.balance, 'Currency')} ${suffix}`;
const balance = this.getBalance(account);
return `${fyo.format(balance, 'Currency')} ${suffix}`;
},
async setTotalDebitAndCredit() {
const totals = await this.fyo.db.getTotalCreditAndDebit();
this.totals = getMapFromList(totals, 'account');
},
async fetchAccounts() {
this.settings = fyo.models[ModelNameEnum.Account].getTreeSettings(fyo);
@ -231,7 +267,6 @@ export default {
'name',
'parentAccount',
'isGroup',
'balance',
'rootType',
'accountType',
],

View File

@ -54,7 +54,6 @@ export class CreateCOA {
parentAccount,
isGroup,
rootType,
balance: 0,
accountType,
});