2022-01-20 20:57:29 +00:00
|
|
|
import frappe from 'frappe';
|
2018-03-26 12:14:27 +00:00
|
|
|
|
2018-03-27 13:52:59 +00:00
|
|
|
class GeneralLedger {
|
2018-07-14 14:28:41 +00:00
|
|
|
async run(params) {
|
|
|
|
const filters = {};
|
|
|
|
if (params.account) filters.account = params.account;
|
|
|
|
if (params.party) filters.party = params.party;
|
2021-12-16 10:57:52 +00:00
|
|
|
if (params.referenceType !== 'All')
|
|
|
|
filters.referenceType = params.referenceType;
|
2018-07-14 14:28:41 +00:00
|
|
|
if (params.referenceName) filters.referenceName = params.referenceName;
|
|
|
|
if (params.toDate || params.fromDate) {
|
|
|
|
filters.date = [];
|
|
|
|
if (params.toDate) filters.date.push('<=', params.toDate);
|
|
|
|
if (params.fromDate) filters.date.push('>=', params.fromDate);
|
|
|
|
}
|
2019-07-18 10:45:44 +00:00
|
|
|
|
2021-12-16 10:26:18 +00:00
|
|
|
let data = (
|
|
|
|
await frappe.db.getAll({
|
|
|
|
doctype: 'AccountingLedgerEntry',
|
|
|
|
fields: [
|
|
|
|
'date',
|
|
|
|
'account',
|
|
|
|
'party',
|
|
|
|
'referenceType',
|
|
|
|
'referenceName',
|
|
|
|
'debit',
|
|
|
|
'credit',
|
|
|
|
'reverted',
|
|
|
|
],
|
|
|
|
filters: filters,
|
|
|
|
})
|
2021-12-29 11:33:58 +00:00
|
|
|
)
|
|
|
|
.filter((d) => !d.reverted || (d.reverted && params.reverted))
|
|
|
|
.map((row) => {
|
|
|
|
row.debit = row.debit.float;
|
|
|
|
row.credit = row.credit.float;
|
|
|
|
|
|
|
|
return row;
|
|
|
|
});
|
2018-03-26 12:14:27 +00:00
|
|
|
|
2019-07-18 06:49:12 +00:00
|
|
|
return this.appendOpeningEntry(data);
|
|
|
|
}
|
|
|
|
appendOpeningEntry(data) {
|
|
|
|
let glEntries = [];
|
|
|
|
let balance = 0,
|
|
|
|
debitTotal = 0,
|
|
|
|
creditTotal = 0;
|
|
|
|
|
|
|
|
glEntries.push({
|
|
|
|
date: '',
|
2019-11-19 19:14:15 +00:00
|
|
|
account: { template: '<b>Opening</b>' },
|
2019-07-18 06:49:12 +00:00
|
|
|
party: '',
|
|
|
|
debit: 0,
|
|
|
|
credit: 0,
|
|
|
|
balance: 0,
|
|
|
|
referenceType: '',
|
2021-12-16 10:26:18 +00:00
|
|
|
referenceName: '',
|
2019-07-18 06:49:12 +00:00
|
|
|
});
|
|
|
|
for (let entry of data) {
|
|
|
|
balance += entry.debit > 0 ? entry.debit : -entry.credit;
|
|
|
|
debitTotal += entry.debit;
|
|
|
|
creditTotal += entry.credit;
|
|
|
|
entry.balance = balance;
|
2019-12-26 14:01:28 +00:00
|
|
|
if (entry.debit === 0) {
|
|
|
|
entry.debit = '';
|
|
|
|
}
|
|
|
|
if (entry.credit === 0) {
|
|
|
|
entry.credit = '';
|
|
|
|
}
|
2019-07-18 06:49:12 +00:00
|
|
|
glEntries.push(entry);
|
|
|
|
}
|
|
|
|
glEntries.push({
|
|
|
|
date: '',
|
2019-11-19 19:14:15 +00:00
|
|
|
account: { template: '<b>Total</b>' },
|
2019-07-18 06:49:12 +00:00
|
|
|
party: '',
|
|
|
|
debit: debitTotal,
|
|
|
|
credit: creditTotal,
|
|
|
|
balance: balance,
|
|
|
|
referenceType: '',
|
2021-12-16 10:26:18 +00:00
|
|
|
referenceName: '',
|
2019-07-18 06:49:12 +00:00
|
|
|
});
|
|
|
|
glEntries.push({
|
|
|
|
date: '',
|
2019-11-19 19:14:15 +00:00
|
|
|
account: { template: '<b>Closing</b>' },
|
2019-07-18 06:49:12 +00:00
|
|
|
party: '',
|
|
|
|
debit: debitTotal,
|
|
|
|
credit: creditTotal,
|
|
|
|
balance: balance,
|
|
|
|
referenceType: '',
|
2021-12-16 10:26:18 +00:00
|
|
|
referenceName: '',
|
2019-07-18 06:49:12 +00:00
|
|
|
});
|
|
|
|
return glEntries;
|
2018-07-14 14:28:41 +00:00
|
|
|
}
|
2018-04-18 09:02:05 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 10:31:26 +00:00
|
|
|
export default GeneralLedger;
|