2022-01-20 20:57:29 +00:00
|
|
|
import frappe from 'frappe';
|
2021-11-04 10:31:26 +00:00
|
|
|
import { DateTime } from 'luxon';
|
2021-12-31 06:53:05 +00:00
|
|
|
import { convertPesaValuesToFloat } from '../../src/utils';
|
2018-04-24 07:58:57 +00:00
|
|
|
|
2021-11-04 10:31:26 +00:00
|
|
|
export async function getData({
|
2019-07-18 10:45:44 +00:00
|
|
|
rootType,
|
|
|
|
balanceMustBe = 'Debit',
|
|
|
|
fromDate,
|
|
|
|
toDate,
|
|
|
|
periodicity = 'Monthly',
|
2021-12-29 11:33:58 +00:00
|
|
|
accumulateValues = false,
|
2018-04-27 11:33:36 +00:00
|
|
|
}) {
|
2019-07-18 10:45:44 +00:00
|
|
|
let accounts = await getAccounts(rootType);
|
|
|
|
let fiscalYear = await getFiscalYear();
|
|
|
|
let ledgerEntries = await getLedgerEntries(fromDate, toDate, accounts);
|
|
|
|
let periodList = getPeriodList(fromDate, toDate, periodicity, fiscalYear);
|
|
|
|
|
|
|
|
for (let account of accounts) {
|
|
|
|
const entries = ledgerEntries.filter(
|
2021-12-29 11:33:58 +00:00
|
|
|
(entry) => entry.account === account.name
|
2019-07-18 10:45:44 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
for (let entry of entries) {
|
|
|
|
let periodKey = getPeriodKey(entry.date, periodicity);
|
|
|
|
|
|
|
|
if (!account[periodKey]) {
|
2021-12-29 11:33:58 +00:00
|
|
|
account[periodKey] = frappe.pesa(0.0);
|
2019-07-18 10:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const multiplier = balanceMustBe === 'Debit' ? 1 : -1;
|
2021-12-29 11:33:58 +00:00
|
|
|
const value = entry.debit.sub(entry.credit).mul(multiplier);
|
|
|
|
account[periodKey] = value.add(account[periodKey]);
|
2019-07-18 10:45:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-24 07:58:57 +00:00
|
|
|
|
2019-07-18 10:45:44 +00:00
|
|
|
if (accumulateValues) {
|
|
|
|
periodList.forEach((periodKey, i) => {
|
|
|
|
if (i === 0) return;
|
|
|
|
const previousPeriodKey = periodList[i - 1];
|
2018-04-24 07:58:57 +00:00
|
|
|
|
2019-07-18 10:45:44 +00:00
|
|
|
for (let account of accounts) {
|
|
|
|
if (!account[periodKey]) {
|
2021-12-29 11:33:58 +00:00
|
|
|
account[periodKey] = frappe.pesa(0.0);
|
2019-07-18 10:45:44 +00:00
|
|
|
}
|
2021-12-29 11:33:58 +00:00
|
|
|
|
|
|
|
account[periodKey] = account[periodKey].add(
|
|
|
|
account[previousPeriodKey] ?? 0
|
|
|
|
);
|
2019-07-18 10:45:44 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-04-24 07:58:57 +00:00
|
|
|
|
2019-07-18 10:45:44 +00:00
|
|
|
// calculate totalRow
|
|
|
|
let totalRow = {
|
2021-12-29 11:33:58 +00:00
|
|
|
account: `Total ${rootType} (${balanceMustBe})`,
|
2019-07-18 10:45:44 +00:00
|
|
|
};
|
2018-04-24 07:58:57 +00:00
|
|
|
|
2021-12-29 11:33:58 +00:00
|
|
|
periodList.forEach((periodKey) => {
|
2019-07-18 10:45:44 +00:00
|
|
|
if (!totalRow[periodKey]) {
|
2021-12-29 11:33:58 +00:00
|
|
|
totalRow[periodKey] = frappe.pesa(0.0);
|
2018-04-24 07:58:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 10:45:44 +00:00
|
|
|
for (let account of accounts) {
|
2021-12-29 11:33:58 +00:00
|
|
|
totalRow[periodKey] = totalRow[periodKey].add(account[periodKey] ?? 0.0);
|
2018-04-24 07:58:57 +00:00
|
|
|
}
|
2019-07-18 10:45:44 +00:00
|
|
|
});
|
2018-04-24 07:58:57 +00:00
|
|
|
|
2021-12-31 06:53:05 +00:00
|
|
|
convertPesaValuesToFloat(totalRow);
|
|
|
|
accounts.forEach(convertPesaValuesToFloat);
|
2021-12-29 11:33:58 +00:00
|
|
|
|
2019-07-18 10:45:44 +00:00
|
|
|
return { accounts, totalRow, periodList };
|
2018-04-24 07:58:57 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 10:31:26 +00:00
|
|
|
export async function getTrialBalance({ rootType, fromDate, toDate }) {
|
2019-07-18 10:45:44 +00:00
|
|
|
let accounts = await getAccounts(rootType);
|
|
|
|
let ledgerEntries = await getLedgerEntries(null, toDate, accounts);
|
|
|
|
|
|
|
|
for (let account of accounts) {
|
|
|
|
const accountEntries = ledgerEntries.filter(
|
2021-12-29 11:33:58 +00:00
|
|
|
(entry) => entry.account === account.name
|
2019-07-18 10:45:44 +00:00
|
|
|
);
|
|
|
|
// opening
|
|
|
|
const beforePeriodEntries = accountEntries.filter(
|
2021-12-29 11:33:58 +00:00
|
|
|
(entry) => entry.date < fromDate
|
|
|
|
);
|
|
|
|
account.opening = beforePeriodEntries.reduce(
|
|
|
|
(acc, entry) => acc.add(entry.debit).sub(entry.credit),
|
|
|
|
frappe.pesa(0)
|
2019-07-18 10:45:44 +00:00
|
|
|
);
|
|
|
|
|
2021-12-29 11:33:58 +00:00
|
|
|
if (account.opening.gte(0)) {
|
2019-07-18 10:45:44 +00:00
|
|
|
account.openingDebit = account.opening;
|
2021-12-29 11:33:58 +00:00
|
|
|
account.openingCredit = frappe.pesa(0);
|
2019-07-18 10:45:44 +00:00
|
|
|
} else {
|
2021-12-29 11:33:58 +00:00
|
|
|
account.openingCredit = account.opening.neg();
|
|
|
|
account.openingDebit = frappe.pesa(0);
|
2019-07-18 10:45:44 +00:00
|
|
|
}
|
2018-04-27 11:33:36 +00:00
|
|
|
|
2019-07-18 10:45:44 +00:00
|
|
|
// debit / credit
|
|
|
|
const periodEntries = accountEntries.filter(
|
2021-12-29 11:33:58 +00:00
|
|
|
(entry) => entry.date >= fromDate && entry.date < toDate
|
|
|
|
);
|
|
|
|
account.debit = periodEntries.reduce(
|
|
|
|
(acc, entry) => acc.add(entry.debit),
|
|
|
|
frappe.pesa(0)
|
2019-07-18 10:45:44 +00:00
|
|
|
);
|
|
|
|
account.credit = periodEntries.reduce(
|
2021-12-29 11:33:58 +00:00
|
|
|
(acc, entry) => acc.add(entry.credit),
|
|
|
|
frappe.pesa(0)
|
2019-07-18 10:45:44 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// closing
|
2021-12-29 11:33:58 +00:00
|
|
|
account.closing = account.opening.add(account.debit).sub(account.credit);
|
2019-07-18 10:45:44 +00:00
|
|
|
|
2021-12-29 11:33:58 +00:00
|
|
|
if (account.closing.gte(0)) {
|
2019-07-18 10:45:44 +00:00
|
|
|
account.closingDebit = account.closing;
|
2021-12-29 11:33:58 +00:00
|
|
|
account.closingCredit = frappe.pesa(0);
|
2019-07-18 10:45:44 +00:00
|
|
|
} else {
|
2021-12-29 11:33:58 +00:00
|
|
|
account.closingCredit = account.closing.neg();
|
|
|
|
account.closingDebit = frappe.pesa(0);
|
2019-07-18 10:45:44 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 11:33:58 +00:00
|
|
|
if (account.debit.neq(0) || account.credit.neq(0)) {
|
2019-07-18 10:45:44 +00:00
|
|
|
setParentEntry(account, account.parentAccount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setParentEntry(leafAccount, parentName) {
|
|
|
|
for (let acc of accounts) {
|
|
|
|
if (acc.name === parentName) {
|
2021-12-29 11:33:58 +00:00
|
|
|
acc.debit = acc.debit.add(leafAccount.debit);
|
|
|
|
acc.credit = acc.credit.add(leafAccount.credit);
|
|
|
|
acc.closing = acc.opening.add(acc.debit).sub(acc.credit);
|
|
|
|
if (acc.closing.gte(0)) {
|
2019-07-18 10:45:44 +00:00
|
|
|
acc.closingDebit = acc.closing;
|
2018-04-27 11:33:36 +00:00
|
|
|
} else {
|
2021-12-29 11:33:58 +00:00
|
|
|
acc.closingCredit = acc.closing.neg();
|
2018-04-27 11:33:36 +00:00
|
|
|
}
|
2019-07-18 10:45:44 +00:00
|
|
|
if (acc.parentAccount) {
|
|
|
|
setParentEntry(leafAccount, acc.parentAccount);
|
2018-04-27 11:33:36 +00:00
|
|
|
} else {
|
2019-07-18 10:45:44 +00:00
|
|
|
return;
|
2018-04-27 11:33:36 +00:00
|
|
|
}
|
2019-07-18 10:45:44 +00:00
|
|
|
}
|
2018-04-27 11:33:36 +00:00
|
|
|
}
|
2019-07-18 10:45:44 +00:00
|
|
|
}
|
2018-04-27 11:33:36 +00:00
|
|
|
|
2021-12-31 06:53:05 +00:00
|
|
|
accounts.forEach(convertPesaValuesToFloat);
|
2019-07-18 10:45:44 +00:00
|
|
|
return accounts;
|
2018-04-27 11:33:36 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 10:31:26 +00:00
|
|
|
export function getPeriodList(fromDate, toDate, periodicity, fiscalYear) {
|
2019-07-18 10:45:44 +00:00
|
|
|
if (!fromDate) {
|
|
|
|
fromDate = fiscalYear.start;
|
|
|
|
}
|
|
|
|
|
|
|
|
let monthsToAdd = {
|
|
|
|
Monthly: 1,
|
|
|
|
Quarterly: 3,
|
|
|
|
'Half Yearly': 6,
|
2021-12-29 11:33:58 +00:00
|
|
|
Yearly: 12,
|
2019-07-18 10:45:44 +00:00
|
|
|
}[periodicity];
|
|
|
|
|
|
|
|
let startDate = DateTime.fromISO(fromDate).startOf('month');
|
|
|
|
let endDate = DateTime.fromISO(toDate).endOf('month');
|
|
|
|
let curDate = startDate;
|
|
|
|
let out = [];
|
|
|
|
|
|
|
|
while (curDate <= endDate) {
|
|
|
|
out.push(getPeriodKey(curDate, periodicity));
|
|
|
|
curDate = curDate.plus({ months: monthsToAdd });
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
2018-04-24 07:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getPeriodKey(date, periodicity) {
|
2019-07-18 10:45:44 +00:00
|
|
|
let key;
|
|
|
|
let dateObj = DateTime.fromISO(date);
|
|
|
|
let year = dateObj.year;
|
|
|
|
let quarter = dateObj.quarter;
|
|
|
|
let month = dateObj.month;
|
|
|
|
|
|
|
|
let getKey = {
|
|
|
|
Monthly: () => `${dateObj.monthShort} ${year}`,
|
|
|
|
Quarterly: () => {
|
|
|
|
return {
|
|
|
|
1: `Jan ${year} - Mar ${year}`,
|
|
|
|
2: `Apr ${year} - Jun ${year}`,
|
|
|
|
3: `Jun ${year} - Sep ${year}`,
|
2021-12-29 11:33:58 +00:00
|
|
|
4: `Oct ${year} - Dec ${year}`,
|
2019-07-18 10:45:44 +00:00
|
|
|
}[quarter];
|
|
|
|
},
|
|
|
|
'Half Yearly': () => {
|
2022-01-12 15:51:42 +00:00
|
|
|
if (month > 3) {
|
|
|
|
return {
|
|
|
|
1: `Apr ${year} - Sep ${year}`,
|
|
|
|
2: `Oct ${year} - Mar ${year + 1}`,
|
|
|
|
}[[2, 3].includes(quarter) ? 1 : 2];
|
|
|
|
}
|
|
|
|
return `Oct ${year - 1} - Mar ${year}`;
|
2019-07-18 10:45:44 +00:00
|
|
|
},
|
|
|
|
Yearly: () => {
|
|
|
|
if (month > 3) {
|
|
|
|
return `${year} - ${year + 1}`;
|
|
|
|
}
|
|
|
|
return `${year - 1} - ${year}`;
|
2021-12-29 11:33:58 +00:00
|
|
|
},
|
2019-07-18 10:45:44 +00:00
|
|
|
}[periodicity];
|
2018-04-24 07:58:57 +00:00
|
|
|
|
2019-07-18 10:45:44 +00:00
|
|
|
return getKey();
|
2018-04-24 07:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setIndentLevel(accounts, parentAccount, level) {
|
2019-07-18 10:45:44 +00:00
|
|
|
if (!parentAccount) {
|
|
|
|
// root
|
|
|
|
parentAccount = null;
|
|
|
|
level = 0;
|
|
|
|
}
|
|
|
|
|
2021-12-29 11:33:58 +00:00
|
|
|
accounts.forEach((account) => {
|
2019-07-18 10:45:44 +00:00
|
|
|
if (
|
|
|
|
account.parentAccount === parentAccount &&
|
|
|
|
account.indent === undefined
|
|
|
|
) {
|
|
|
|
account.indent = level;
|
|
|
|
setIndentLevel(accounts, account.name, level + 1);
|
2018-04-24 07:58:57 +00:00
|
|
|
}
|
2019-07-18 10:45:44 +00:00
|
|
|
});
|
2018-04-24 07:58:57 +00:00
|
|
|
|
2019-07-18 10:45:44 +00:00
|
|
|
return accounts;
|
2018-04-24 07:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function sortAccounts(accounts) {
|
2019-07-18 10:45:44 +00:00
|
|
|
let out = [];
|
|
|
|
let pushed = {};
|
2018-04-24 07:58:57 +00:00
|
|
|
|
2019-07-18 10:45:44 +00:00
|
|
|
pushToOut(null);
|
2018-04-24 07:58:57 +00:00
|
|
|
|
2019-07-18 10:45:44 +00:00
|
|
|
function pushToOut(parentAccount) {
|
2021-12-29 11:33:58 +00:00
|
|
|
accounts.forEach((account) => {
|
2019-07-18 10:45:44 +00:00
|
|
|
if (account.parentAccount === parentAccount && !pushed[account.name]) {
|
|
|
|
out.push(account);
|
|
|
|
pushed[account.name] = 1;
|
2018-04-24 07:58:57 +00:00
|
|
|
|
2019-07-18 10:45:44 +00:00
|
|
|
pushToOut(account.name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-04-24 07:58:57 +00:00
|
|
|
|
2019-07-18 10:45:44 +00:00
|
|
|
return out;
|
2018-04-24 07:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getLedgerEntries(fromDate, toDate, accounts) {
|
2019-07-18 10:45:44 +00:00
|
|
|
const dateFilter = () => {
|
|
|
|
const before = ['<=', toDate];
|
|
|
|
const after = ['>=', fromDate];
|
|
|
|
if (fromDate) {
|
|
|
|
return [...after, ...before];
|
2018-04-24 07:58:57 +00:00
|
|
|
}
|
2019-07-18 10:45:44 +00:00
|
|
|
return before;
|
|
|
|
};
|
|
|
|
|
|
|
|
const ledgerEntries = await frappe.db.getAll({
|
|
|
|
doctype: 'AccountingLedgerEntry',
|
|
|
|
fields: ['account', 'debit', 'credit', 'date'],
|
|
|
|
filters: {
|
2021-12-29 11:33:58 +00:00
|
|
|
account: ['in', accounts.map((d) => d.name)],
|
|
|
|
date: dateFilter(),
|
|
|
|
},
|
2019-07-18 10:45:44 +00:00
|
|
|
});
|
2018-04-24 07:58:57 +00:00
|
|
|
|
2019-07-18 10:45:44 +00:00
|
|
|
return ledgerEntries;
|
2018-04-24 07:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getAccounts(rootType) {
|
2019-07-18 10:45:44 +00:00
|
|
|
let accounts = await frappe.db.getAll({
|
|
|
|
doctype: 'Account',
|
2019-12-03 11:45:07 +00:00
|
|
|
fields: ['name', 'parentAccount', 'isGroup'],
|
2019-07-18 10:45:44 +00:00
|
|
|
filters: {
|
2021-12-29 11:33:58 +00:00
|
|
|
rootType,
|
|
|
|
},
|
2019-07-18 10:45:44 +00:00
|
|
|
});
|
2018-04-24 07:58:57 +00:00
|
|
|
|
2019-07-18 10:45:44 +00:00
|
|
|
accounts = setIndentLevel(accounts);
|
|
|
|
accounts = sortAccounts(accounts);
|
2018-04-24 07:58:57 +00:00
|
|
|
|
2021-12-29 11:33:58 +00:00
|
|
|
accounts.forEach((account) => {
|
2019-07-18 10:45:44 +00:00
|
|
|
account.account = account.name;
|
|
|
|
});
|
2018-04-24 07:58:57 +00:00
|
|
|
|
2019-07-18 10:45:44 +00:00
|
|
|
return accounts;
|
2018-04-24 07:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getFiscalYear() {
|
2019-07-18 10:45:44 +00:00
|
|
|
let { fiscalYearStart, fiscalYearEnd } = await frappe.getSingle(
|
|
|
|
'AccountingSettings'
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
start: fiscalYearStart,
|
2021-12-29 11:33:58 +00:00
|
|
|
end: fiscalYearEnd,
|
2019-07-18 10:45:44 +00:00
|
|
|
};
|
2018-04-24 07:58:57 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 10:31:26 +00:00
|
|
|
export default {
|
2019-07-18 10:45:44 +00:00
|
|
|
getData,
|
2019-12-04 17:23:28 +00:00
|
|
|
getTrialBalance,
|
2021-12-29 11:33:58 +00:00
|
|
|
getPeriodList,
|
2019-07-18 10:45:44 +00:00
|
|
|
};
|