2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 11:29:00 +00:00

fix: use neg, neq, update reports GL, PnL, BS, TB

This commit is contained in:
18alantom 2021-12-29 17:03:58 +05:30
parent 373684aab7
commit de17463bd2
3 changed files with 81 additions and 51 deletions

View File

@ -29,13 +29,13 @@ export default class LedgerPosting {
const debitAccounts = ['Asset', 'Expense']; const debitAccounts = ['Asset', 'Expense'];
const { rootType } = await frappe.getDoc('Account', accountName); const { rootType } = await frappe.getDoc('Account', accountName);
if (debitAccounts.indexOf(rootType) === -1) { if (debitAccounts.indexOf(rootType) === -1) {
const change = type == 'credit' ? amount : amount.mul(-1); const change = type == 'credit' ? amount : amount.neg();
this.accountEntries.push({ this.accountEntries.push({
name: accountName, name: accountName,
balanceChange: change, balanceChange: change,
}); });
} else { } else {
const change = type == 'debit' ? amount : amount.mul(-1); const change = type == 'debit' ? amount : amount.neg();
this.accountEntries.push({ this.accountEntries.push({
name: accountName, name: accountName,
balanceChange: change, balanceChange: change,
@ -95,7 +95,7 @@ export default class LedgerPosting {
entry.reverted = 1; entry.reverted = 1;
} }
for (let entry of this.accountEntries) { for (let entry of this.accountEntries) {
entry.balanceChange = entry.balanceChange.mul(-1); entry.balanceChange = entry.balanceChange.neg();
} }
await this.insertEntries(); await this.insertEntries();
} }
@ -121,7 +121,7 @@ export default class LedgerPosting {
validateEntries() { validateEntries() {
let { debit, credit } = this.getTotalDebitAndCredit(); let { debit, credit } = this.getTotalDebitAndCredit();
if (!debit.eq(credit)) { if (debit.neq(credit)) {
throw new Error( throw new Error(
`Total Debit: ${frappe.format( `Total Debit: ${frappe.format(
debit, debit,

View File

@ -1,6 +1,6 @@
import frappe from 'frappejs'; import frappe from 'frappejs';
import { isPesa } from 'frappejs/utils';
import { DateTime } from 'luxon'; import { DateTime } from 'luxon';
import { unique } from 'frappejs/utils';
export async function getData({ export async function getData({
rootType, rootType,
@ -8,7 +8,7 @@ export async function getData({
fromDate, fromDate,
toDate, toDate,
periodicity = 'Monthly', periodicity = 'Monthly',
accumulateValues = false accumulateValues = false,
}) { }) {
let accounts = await getAccounts(rootType); let accounts = await getAccounts(rootType);
let fiscalYear = await getFiscalYear(); let fiscalYear = await getFiscalYear();
@ -17,19 +17,19 @@ export async function getData({
for (let account of accounts) { for (let account of accounts) {
const entries = ledgerEntries.filter( const entries = ledgerEntries.filter(
entry => entry.account === account.name (entry) => entry.account === account.name
); );
for (let entry of entries) { for (let entry of entries) {
let periodKey = getPeriodKey(entry.date, periodicity); let periodKey = getPeriodKey(entry.date, periodicity);
if (!account[periodKey]) { if (!account[periodKey]) {
account[periodKey] = 0.0; account[periodKey] = frappe.pesa(0.0);
} }
const multiplier = balanceMustBe === 'Debit' ? 1 : -1; const multiplier = balanceMustBe === 'Debit' ? 1 : -1;
const value = (entry.debit - entry.credit) * multiplier; const value = entry.debit.sub(entry.credit).mul(multiplier);
account[periodKey] += value; account[periodKey] = value.add(account[periodKey]);
} }
} }
@ -40,28 +40,34 @@ export async function getData({
for (let account of accounts) { for (let account of accounts) {
if (!account[periodKey]) { if (!account[periodKey]) {
account[periodKey] = 0.0; account[periodKey] = frappe.pesa(0.0);
} }
account[periodKey] += account[previousPeriodKey] || 0.0;
account[periodKey] = account[periodKey].add(
account[previousPeriodKey] ?? 0
);
} }
}); });
} }
// calculate totalRow // calculate totalRow
let totalRow = { let totalRow = {
account: `Total ${rootType} (${balanceMustBe})` account: `Total ${rootType} (${balanceMustBe})`,
}; };
periodList.forEach(periodKey => { periodList.forEach((periodKey) => {
if (!totalRow[periodKey]) { if (!totalRow[periodKey]) {
totalRow[periodKey] = 0.0; totalRow[periodKey] = frappe.pesa(0.0);
} }
for (let account of accounts) { for (let account of accounts) {
totalRow[periodKey] += account[periodKey] || 0.0; totalRow[periodKey] = totalRow[periodKey].add(account[periodKey] ?? 0.0);
} }
}); });
convertToFloat(totalRow);
accounts.forEach(convertToFloat);
return { accounts, totalRow, periodList }; return { accounts, totalRow, periodList };
} }
@ -71,42 +77,50 @@ export async function getTrialBalance({ rootType, fromDate, toDate }) {
for (let account of accounts) { for (let account of accounts) {
const accountEntries = ledgerEntries.filter( const accountEntries = ledgerEntries.filter(
entry => entry.account === account.name (entry) => entry.account === account.name
); );
// opening // opening
const beforePeriodEntries = accountEntries.filter( const beforePeriodEntries = accountEntries.filter(
entry => entry.date < fromDate (entry) => entry.date < fromDate
);
account.opening = beforePeriodEntries.reduce(
(acc, entry) => acc.add(entry.debit).sub(entry.credit),
frappe.pesa(0)
); );
account.opening = beforePeriodEntries.reduce((acc, entry) => {
return acc + (entry.debit - entry.credit);
}, 0);
if (account.opening >= 0) { if (account.opening.gte(0)) {
account.openingDebit = account.opening; account.openingDebit = account.opening;
account.openingCredit = frappe.pesa(0);
} else { } else {
account.openingCredit = -account.opening; account.openingCredit = account.opening.neg();
account.openingDebit = frappe.pesa(0);
} }
// debit / credit // debit / credit
const periodEntries = accountEntries.filter( const periodEntries = accountEntries.filter(
entry => entry.date >= fromDate && entry.date < toDate (entry) => entry.date >= fromDate && entry.date < toDate
);
account.debit = periodEntries.reduce(
(acc, entry) => acc.add(entry.debit),
frappe.pesa(0)
); );
account.debit = periodEntries.reduce((acc, entry) => acc + entry.debit, 0);
account.credit = periodEntries.reduce( account.credit = periodEntries.reduce(
(acc, entry) => acc + entry.credit, (acc, entry) => acc.add(entry.credit),
0 frappe.pesa(0)
); );
// closing // closing
account.closing = account.opening + account.debit - account.credit; account.closing = account.opening.add(account.debit).sub(account.credit);
if (account.closing >= 0) { if (account.closing.gte(0)) {
account.closingDebit = account.closing; account.closingDebit = account.closing;
account.closingCredit = frappe.pesa(0);
} else { } else {
account.closingCredit = -account.closing; account.closingCredit = account.closing.neg();
account.closingDebit = frappe.pesa(0);
} }
if (account.debit != 0 || account.credit != 0) { if (account.debit.neq(0) || account.credit.neq(0)) {
setParentEntry(account, account.parentAccount); setParentEntry(account, account.parentAccount);
} }
} }
@ -114,13 +128,13 @@ export async function getTrialBalance({ rootType, fromDate, toDate }) {
function setParentEntry(leafAccount, parentName) { function setParentEntry(leafAccount, parentName) {
for (let acc of accounts) { for (let acc of accounts) {
if (acc.name === parentName) { if (acc.name === parentName) {
acc.debit += leafAccount.debit; acc.debit = acc.debit.add(leafAccount.debit);
acc.credit += leafAccount.credit; acc.credit = acc.credit.add(leafAccount.credit);
acc.closing = acc.opening + acc.debit - acc.credit; acc.closing = acc.opening.add(acc.debit).sub(acc.credit);
if (acc.closing >= 0) { if (acc.closing.gte(0)) {
acc.closingDebit = acc.closing; acc.closingDebit = acc.closing;
} else { } else {
acc.closingCredit = -acc.closing; acc.closingCredit = acc.closing.neg();
} }
if (acc.parentAccount) { if (acc.parentAccount) {
setParentEntry(leafAccount, acc.parentAccount); setParentEntry(leafAccount, acc.parentAccount);
@ -131,6 +145,7 @@ export async function getTrialBalance({ rootType, fromDate, toDate }) {
} }
} }
accounts.forEach(convertToFloat);
return accounts; return accounts;
} }
@ -143,7 +158,7 @@ export function getPeriodList(fromDate, toDate, periodicity, fiscalYear) {
Monthly: 1, Monthly: 1,
Quarterly: 3, Quarterly: 3,
'Half Yearly': 6, 'Half Yearly': 6,
Yearly: 12 Yearly: 12,
}[periodicity]; }[periodicity];
let startDate = DateTime.fromISO(fromDate).startOf('month'); let startDate = DateTime.fromISO(fromDate).startOf('month');
@ -173,13 +188,13 @@ function getPeriodKey(date, periodicity) {
1: `Jan ${year} - Mar ${year}`, 1: `Jan ${year} - Mar ${year}`,
2: `Apr ${year} - Jun ${year}`, 2: `Apr ${year} - Jun ${year}`,
3: `Jun ${year} - Sep ${year}`, 3: `Jun ${year} - Sep ${year}`,
4: `Oct ${year} - Dec ${year}` 4: `Oct ${year} - Dec ${year}`,
}[quarter]; }[quarter];
}, },
'Half Yearly': () => { 'Half Yearly': () => {
return { return {
1: `Apr ${year} - Sep ${year}`, 1: `Apr ${year} - Sep ${year}`,
2: `Oct ${year} - Mar ${year}` 2: `Oct ${year} - Mar ${year}`,
}[[2, 3].includes(quarter) ? 1 : 2]; }[[2, 3].includes(quarter) ? 1 : 2];
}, },
Yearly: () => { Yearly: () => {
@ -187,7 +202,7 @@ function getPeriodKey(date, periodicity) {
return `${year} - ${year + 1}`; return `${year} - ${year + 1}`;
} }
return `${year - 1} - ${year}`; return `${year - 1} - ${year}`;
} },
}[periodicity]; }[periodicity];
return getKey(); return getKey();
@ -200,7 +215,7 @@ function setIndentLevel(accounts, parentAccount, level) {
level = 0; level = 0;
} }
accounts.forEach(account => { accounts.forEach((account) => {
if ( if (
account.parentAccount === parentAccount && account.parentAccount === parentAccount &&
account.indent === undefined account.indent === undefined
@ -220,7 +235,7 @@ function sortAccounts(accounts) {
pushToOut(null); pushToOut(null);
function pushToOut(parentAccount) { function pushToOut(parentAccount) {
accounts.forEach(account => { accounts.forEach((account) => {
if (account.parentAccount === parentAccount && !pushed[account.name]) { if (account.parentAccount === parentAccount && !pushed[account.name]) {
out.push(account); out.push(account);
pushed[account.name] = 1; pushed[account.name] = 1;
@ -247,9 +262,9 @@ async function getLedgerEntries(fromDate, toDate, accounts) {
doctype: 'AccountingLedgerEntry', doctype: 'AccountingLedgerEntry',
fields: ['account', 'debit', 'credit', 'date'], fields: ['account', 'debit', 'credit', 'date'],
filters: { filters: {
account: ['in', accounts.map(d => d.name)], account: ['in', accounts.map((d) => d.name)],
date: dateFilter() date: dateFilter(),
} },
}); });
return ledgerEntries; return ledgerEntries;
@ -260,14 +275,14 @@ async function getAccounts(rootType) {
doctype: 'Account', doctype: 'Account',
fields: ['name', 'parentAccount', 'isGroup'], fields: ['name', 'parentAccount', 'isGroup'],
filters: { filters: {
rootType rootType,
} },
}); });
accounts = setIndentLevel(accounts); accounts = setIndentLevel(accounts);
accounts = sortAccounts(accounts); accounts = sortAccounts(accounts);
accounts.forEach(account => { accounts.forEach((account) => {
account.account = account.name; account.account = account.name;
}); });
@ -280,12 +295,20 @@ async function getFiscalYear() {
); );
return { return {
start: fiscalYearStart, start: fiscalYearStart,
end: fiscalYearEnd end: fiscalYearEnd,
}; };
} }
function convertToFloat(obj) {
Object.keys(obj).forEach((key) => {
if (!isPesa(obj[key])) return;
obj[key] = obj[key].float;
});
}
export default { export default {
getData, getData,
getTrialBalance, getTrialBalance,
getPeriodList getPeriodList,
}; };

View File

@ -29,7 +29,14 @@ class GeneralLedger {
], ],
filters: filters, filters: filters,
}) })
).filter((d) => !d.reverted || (d.reverted && params.reverted)); )
.filter((d) => !d.reverted || (d.reverted && params.reverted))
.map((row) => {
row.debit = row.debit.float;
row.credit = row.credit.float;
return row;
});
return this.appendOpeningEntry(data); return this.appendOpeningEntry(data);
} }