mirror of
https://github.com/frappe/books.git
synced 2024-11-09 23:30:56 +00:00
refactor: remove account balance field
This commit is contained in:
parent
d844cc6816
commit
2bc3df90a1
@ -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<string, AccountingLedgerEntry>;
|
||||
debitMap: Record<string, AccountingLedgerEntry>;
|
||||
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() {
|
||||
|
@ -17,9 +17,4 @@ export interface LedgerEntry {
|
||||
credit: Money;
|
||||
}
|
||||
|
||||
export interface AccountBalanceChange {
|
||||
name: string;
|
||||
change: Money;
|
||||
}
|
||||
|
||||
export type TransactionType = 'credit' | 'debit';
|
||||
|
@ -130,12 +130,6 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"fieldname": "balance",
|
||||
"label": "Balance",
|
||||
"fieldtype": "Currency",
|
||||
"readOnly": true
|
||||
},
|
||||
{
|
||||
"fieldname": "isGroup",
|
||||
"label": "Is Group",
|
||||
|
Loading…
Reference in New Issue
Block a user