mirror of
https://github.com/frappe/books.git
synced 2024-11-08 14:50: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 { Fyo, t } from 'fyo';
|
||||||
import { NotFoundError, ValidationError } from 'fyo/utils/errors';
|
import { NotFoundError, ValidationError } from 'fyo/utils/errors';
|
||||||
import { Account } from 'models/baseModels/Account/Account';
|
|
||||||
import { AccountingLedgerEntry } from 'models/baseModels/AccountingLedgerEntry/AccountingLedgerEntry';
|
import { AccountingLedgerEntry } from 'models/baseModels/AccountingLedgerEntry/AccountingLedgerEntry';
|
||||||
import { ModelNameEnum } from 'models/types';
|
import { ModelNameEnum } from 'models/types';
|
||||||
import { Money } from 'pesa';
|
import { Money } from 'pesa';
|
||||||
import { Transactional } from './Transactional';
|
import { Transactional } from './Transactional';
|
||||||
import { AccountBalanceChange, TransactionType } from './types';
|
import { TransactionType } from './types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* # LedgerPosting
|
* # LedgerPosting
|
||||||
@ -16,8 +15,7 @@ import { AccountBalanceChange, TransactionType } from './types';
|
|||||||
* For each account touched in a transactional doc a separate ledger entry is
|
* For each account touched in a transactional doc a separate ledger entry is
|
||||||
* created.
|
* created.
|
||||||
*
|
*
|
||||||
* This is done using the `debit(...)` and `credit(...)` methods which also
|
* This is done using the `debit(...)` and `credit(...)` methods.
|
||||||
* keep track of the change in balance to an account.
|
|
||||||
*
|
*
|
||||||
* Unless `post()` or `postReverese()` is called the entries aren't made.
|
* Unless `post()` or `postReverese()` is called the entries aren't made.
|
||||||
*/
|
*/
|
||||||
@ -29,7 +27,6 @@ export class LedgerPosting {
|
|||||||
creditMap: Record<string, AccountingLedgerEntry>;
|
creditMap: Record<string, AccountingLedgerEntry>;
|
||||||
debitMap: Record<string, AccountingLedgerEntry>;
|
debitMap: Record<string, AccountingLedgerEntry>;
|
||||||
reverted: boolean;
|
reverted: boolean;
|
||||||
accountBalanceChanges: AccountBalanceChange[];
|
|
||||||
|
|
||||||
constructor(refDoc: Transactional, fyo: Fyo) {
|
constructor(refDoc: Transactional, fyo: Fyo) {
|
||||||
this.fyo = fyo;
|
this.fyo = fyo;
|
||||||
@ -38,19 +35,16 @@ export class LedgerPosting {
|
|||||||
this.creditMap = {};
|
this.creditMap = {};
|
||||||
this.debitMap = {};
|
this.debitMap = {};
|
||||||
this.reverted = false;
|
this.reverted = false;
|
||||||
this.accountBalanceChanges = [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async debit(account: string, amount: Money) {
|
async debit(account: string, amount: Money) {
|
||||||
const ledgerEntry = this._getLedgerEntry(account, 'debit');
|
const ledgerEntry = this._getLedgerEntry(account, 'debit');
|
||||||
await ledgerEntry.set('debit', ledgerEntry.debit!.add(amount));
|
await ledgerEntry.set('debit', ledgerEntry.debit!.add(amount));
|
||||||
await this._updateAccountBalanceChange(account, 'debit', amount);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async credit(account: string, amount: Money) {
|
async credit(account: string, amount: Money) {
|
||||||
const ledgerEntry = this._getLedgerEntry(account, 'credit');
|
const ledgerEntry = this._getLedgerEntry(account, 'credit');
|
||||||
await ledgerEntry.set('credit', ledgerEntry.credit!.add(amount));
|
await ledgerEntry.set('credit', ledgerEntry.credit!.add(amount));
|
||||||
await this._updateAccountBalanceChange(account, 'credit', amount);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async post() {
|
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(
|
_getLedgerEntry(
|
||||||
account: string,
|
account: string,
|
||||||
type: TransactionType
|
type: TransactionType
|
||||||
@ -165,7 +139,6 @@ export class LedgerPosting {
|
|||||||
|
|
||||||
async _sync() {
|
async _sync() {
|
||||||
await this._syncLedgerEntries();
|
await this._syncLedgerEntries();
|
||||||
await this._syncBalanceChanges();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async _syncLedgerEntries() {
|
async _syncLedgerEntries() {
|
||||||
@ -176,18 +149,6 @@ export class LedgerPosting {
|
|||||||
|
|
||||||
async _syncReverse() {
|
async _syncReverse() {
|
||||||
await this._syncReverseLedgerEntries();
|
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() {
|
async _syncReverseLedgerEntries() {
|
||||||
|
@ -17,9 +17,4 @@ export interface LedgerEntry {
|
|||||||
credit: Money;
|
credit: Money;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AccountBalanceChange {
|
|
||||||
name: string;
|
|
||||||
change: Money;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type TransactionType = 'credit' | 'debit';
|
export type TransactionType = 'credit' | 'debit';
|
||||||
|
@ -130,12 +130,6 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"fieldname": "balance",
|
|
||||||
"label": "Balance",
|
|
||||||
"fieldtype": "Currency",
|
|
||||||
"readOnly": true
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"fieldname": "isGroup",
|
"fieldname": "isGroup",
|
||||||
"label": "Is Group",
|
"label": "Is Group",
|
||||||
|
Loading…
Reference in New Issue
Block a user