2018-03-26 12:14:27 +00:00
|
|
|
const frappe = require('frappejs');
|
|
|
|
|
2018-03-27 13:52:59 +00:00
|
|
|
class GeneralLedger {
|
2018-07-14 14:28:41 +00:00
|
|
|
async run(params) {
|
2019-08-14 07:43:49 +00:00
|
|
|
if (!Object.keys(params).length) return [];
|
|
|
|
|
2018-07-14 14:28:41 +00:00
|
|
|
const filters = {};
|
|
|
|
if (params.account) filters.account = params.account;
|
|
|
|
if (params.party) filters.party = params.party;
|
|
|
|
if (params.referenceType) filters.referenceType = params.referenceType;
|
|
|
|
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
|
|
|
|
2018-07-14 14:28:41 +00:00
|
|
|
let data = await frappe.db.getAll({
|
|
|
|
doctype: 'AccountingLedgerEntry',
|
2019-07-18 06:49:12 +00:00
|
|
|
fields: [
|
|
|
|
'date',
|
|
|
|
'account',
|
|
|
|
'party',
|
|
|
|
'referenceType',
|
|
|
|
'referenceName',
|
|
|
|
'debit',
|
|
|
|
'credit'
|
|
|
|
],
|
2018-07-14 14:28:41 +00:00
|
|
|
filters: filters
|
|
|
|
});
|
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-07-18 10:45:44 +00:00
|
|
|
account: '<b>Opening</b>',
|
2019-07-18 06:49:12 +00:00
|
|
|
party: '',
|
|
|
|
debit: 0,
|
|
|
|
credit: 0,
|
|
|
|
balance: 0,
|
|
|
|
referenceType: '',
|
|
|
|
referenceName: ''
|
|
|
|
});
|
|
|
|
for (let entry of data) {
|
|
|
|
balance += entry.debit > 0 ? entry.debit : -entry.credit;
|
|
|
|
debitTotal += entry.debit;
|
|
|
|
creditTotal += entry.credit;
|
|
|
|
entry.balance = balance;
|
|
|
|
glEntries.push(entry);
|
|
|
|
}
|
|
|
|
glEntries.push({
|
|
|
|
date: '',
|
2019-07-18 10:45:44 +00:00
|
|
|
account: '<b>Total</b>',
|
2019-07-18 06:49:12 +00:00
|
|
|
party: '',
|
|
|
|
debit: debitTotal,
|
|
|
|
credit: creditTotal,
|
|
|
|
balance: balance,
|
|
|
|
referenceType: '',
|
|
|
|
referenceName: ''
|
|
|
|
});
|
|
|
|
glEntries.push({
|
|
|
|
date: '',
|
2019-07-18 10:45:44 +00:00
|
|
|
account: '<b>Closing</b>',
|
2019-07-18 06:49:12 +00:00
|
|
|
party: '',
|
|
|
|
debit: debitTotal,
|
|
|
|
credit: creditTotal,
|
|
|
|
balance: balance,
|
|
|
|
referenceType: '',
|
|
|
|
referenceName: ''
|
|
|
|
});
|
|
|
|
return glEntries;
|
2018-07-14 14:28:41 +00:00
|
|
|
}
|
2018-04-18 09:02:05 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 10:23:27 +00:00
|
|
|
module.exports = GeneralLedger;
|