From 2bc3df90a1db6fb98480bae187f3abad5bb038fa Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Mon, 7 Nov 2022 12:43:01 +0530 Subject: [PATCH] refactor: remove account balance field --- models/Transactional/LedgerPosting.ts | 43 ++------------------------- models/Transactional/types.ts | 5 ---- schemas/app/Account.json | 6 ---- 3 files changed, 2 insertions(+), 52 deletions(-) diff --git a/models/Transactional/LedgerPosting.ts b/models/Transactional/LedgerPosting.ts index e22719e2..a448a279 100644 --- a/models/Transactional/LedgerPosting.ts +++ b/models/Transactional/LedgerPosting.ts @@ -1,11 +1,10 @@ import { Fyo, t } from 'fyo'; import { NotFoundError, ValidationError } from 'fyo/utils/errors'; -import { Account } from 'models/baseModels/Account/Account'; import { AccountingLedgerEntry } from 'models/baseModels/AccountingLedgerEntry/AccountingLedgerEntry'; import { ModelNameEnum } from 'models/types'; import { Money } from 'pesa'; import { Transactional } from './Transactional'; -import { AccountBalanceChange, TransactionType } from './types'; +import { TransactionType } from './types'; /** * # LedgerPosting @@ -16,8 +15,7 @@ import { AccountBalanceChange, TransactionType } from './types'; * For each account touched in a transactional doc a separate ledger entry is * created. * - * This is done using the `debit(...)` and `credit(...)` methods which also - * keep track of the change in balance to an account. + * This is done using the `debit(...)` and `credit(...)` methods. * * Unless `post()` or `postReverese()` is called the entries aren't made. */ @@ -29,7 +27,6 @@ export class LedgerPosting { creditMap: Record; debitMap: Record; reverted: boolean; - accountBalanceChanges: AccountBalanceChange[]; constructor(refDoc: Transactional, fyo: Fyo) { this.fyo = fyo; @@ -38,19 +35,16 @@ export class LedgerPosting { this.creditMap = {}; this.debitMap = {}; this.reverted = false; - this.accountBalanceChanges = []; } async debit(account: string, amount: Money) { const ledgerEntry = this._getLedgerEntry(account, 'debit'); await ledgerEntry.set('debit', ledgerEntry.debit!.add(amount)); - await this._updateAccountBalanceChange(account, 'debit', amount); } async credit(account: string, amount: Money) { const ledgerEntry = this._getLedgerEntry(account, 'credit'); await ledgerEntry.set('credit', ledgerEntry.credit!.add(amount)); - await this._updateAccountBalanceChange(account, 'credit', amount); } async post() { @@ -83,26 +77,6 @@ export class LedgerPosting { } } - async _updateAccountBalanceChange( - name: string, - type: TransactionType, - amount: Money - ) { - const accountDoc = (await this.fyo.doc.getDoc('Account', name)) as Account; - - let change: Money; - if (accountDoc.isDebit) { - change = type === 'debit' ? amount : amount.neg(); - } else { - change = type === 'credit' ? amount : amount.neg(); - } - - this.accountBalanceChanges.push({ - name, - change, - }); - } - _getLedgerEntry( account: string, type: TransactionType @@ -165,7 +139,6 @@ export class LedgerPosting { async _sync() { await this._syncLedgerEntries(); - await this._syncBalanceChanges(); } async _syncLedgerEntries() { @@ -176,18 +149,6 @@ export class LedgerPosting { async _syncReverse() { await this._syncReverseLedgerEntries(); - for (const entry of this.accountBalanceChanges) { - entry.change = (entry.change as Money).neg(); - } - await this._syncBalanceChanges(); - } - - async _syncBalanceChanges() { - for (const { name, change } of this.accountBalanceChanges) { - const accountDoc = await this.fyo.doc.getDoc(ModelNameEnum.Account, name); - const balance = accountDoc.get('balance') as Money; - await accountDoc.setAndSync('balance', balance.add(change)); - } } async _syncReverseLedgerEntries() { diff --git a/models/Transactional/types.ts b/models/Transactional/types.ts index 721cf5a3..7a9f96f6 100644 --- a/models/Transactional/types.ts +++ b/models/Transactional/types.ts @@ -17,9 +17,4 @@ export interface LedgerEntry { credit: Money; } -export interface AccountBalanceChange { - name: string; - change: Money; -} - export type TransactionType = 'credit' | 'debit'; diff --git a/schemas/app/Account.json b/schemas/app/Account.json index d9f861d1..400ee5bd 100644 --- a/schemas/app/Account.json +++ b/schemas/app/Account.json @@ -130,12 +130,6 @@ } ] }, - { - "fieldname": "balance", - "label": "Balance", - "fieldtype": "Currency", - "readOnly": true - }, { "fieldname": "isGroup", "label": "Is Group",