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

Merge pull request #375 from 4silvertooth/master

fix: #176 erroneous Profit ans Loss reports
This commit is contained in:
Alan 2022-04-04 12:25:04 +05:30 committed by GitHub
commit 54628bb2d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 25 deletions

View File

@ -1,6 +1,6 @@
import frappe from 'frappe'; import frappe from 'frappe';
import { DateTime } from 'luxon'; import { DateTime } from 'luxon';
import { getPeriodList } from '../FinancialStatements/FinancialStatements'; import { getPeriodList, getFiscalYear } from '../FinancialStatements/FinancialStatements';
class Cashflow { class Cashflow {
async run({ fromDate, toDate, periodicity }) { async run({ fromDate, toDate, periodicity }) {
@ -24,7 +24,8 @@ class Cashflow {
.whereBetween('date', [fromDate, toDate]) .whereBetween('date', [fromDate, toDate])
.groupBy(dateAsMonthYear); .groupBy(dateAsMonthYear);
let periodList = getPeriodList(fromDate, toDate, periodicity); let fiscalYear = await getFiscalYear();
let periodList = getPeriodList(fromDate, toDate, periodicity, fiscalYear);
let data = periodList.map((periodKey) => { let data = periodList.map((periodKey) => {
let monthYear = this.getMonthYear(periodKey, 'MMM yyyy'); let monthYear = this.getMonthYear(periodKey, 'MMM yyyy');

View File

@ -21,7 +21,7 @@ export async function getData({
); );
for (let entry of entries) { for (let entry of entries) {
let periodKey = getPeriodKey(entry.date, periodicity); let periodKey = getPeriodKey(entry.date, periodicity, fiscalYear);
if (!account[periodKey]) { if (!account[periodKey]) {
account[periodKey] = frappe.pesa(0.0); account[periodKey] = frappe.pesa(0.0);
@ -167,44 +167,47 @@ export function getPeriodList(fromDate, toDate, periodicity, fiscalYear) {
let out = []; let out = [];
while (curDate <= endDate) { while (curDate <= endDate) {
out.push(getPeriodKey(curDate, periodicity)); out.push(getPeriodKey(curDate, periodicity, fiscalYear));
curDate = curDate.plus({ months: monthsToAdd }); curDate = curDate.plus({ months: monthsToAdd });
} }
return out; return out;
} }
function getPeriodKey(date, periodicity) { function getPeriodKey(date, periodicity, fiscalYear) {
let key; let key;
let { start, end, quarters, isSplit } = fiscalYear;
let dateObj = DateTime.fromISO(date); let dateObj = DateTime.fromISO(date);
let year = dateObj.year; let { month, quarter, year } = dateObj;
let quarter = dateObj.quarter; let fisacalStart = DateTime.fromISO(start);
let month = dateObj.month; let fisacalEnd = DateTime.fromISO(end);
let getKey = { let getKey = {
Monthly: () => `${dateObj.monthShort} ${year}`, Monthly: () => `${dateObj.monthShort} ${year}`,
Quarterly: () => { Quarterly: () => {
const key = month < fisacalStart.month ? `${year-1} - ${year}` : `${year} - ${year+1}`;
let strYear = isSplit ? key : `${year}`;
return { return {
1: `Jan ${year} - Mar ${year}`, 1: `Q1 ${strYear}`,
2: `Apr ${year} - Jun ${year}`, 2: `Q2 ${strYear}`,
3: `Jun ${year} - Sep ${year}`, 3: `Q3 ${strYear}`,
4: `Oct ${year} - Dec ${year}`, 4: `Q4 ${strYear}`,
}[quarter]; }[quarters[month-1]];
}, },
'Half Yearly': () => { 'Half Yearly': () => {
if (month > 3) { const key = month < fisacalStart.month ? `${year-1} - ${year}` : `${year} - ${year+1}`;
let strYear = isSplit ? key : `${year}`;
return { return {
1: `Apr ${year} - Sep ${year}`, 1: `1st Half ${strYear}`,
2: `Oct ${year} - Mar ${year + 1}`, 2: `1st Half ${strYear}`,
}[[2, 3].includes(quarter) ? 1 : 2]; 3: `2nd Half ${strYear}`,
} 4: `2nd Half ${strYear}`,
return `Oct ${year - 1} - Mar ${year}`; }[quarters[month-1]];
}, },
Yearly: () => { Yearly: () => {
if (month > 3) { const key = month < fisacalStart.month ? `${year-1} - ${year}` : `${year} - ${year+1}`;
return `${year} - ${year + 1}`; let strYear = isSplit ? key : `${year}`;
} return `FY ${strYear}`;
return `${year - 1} - ${year}`;
}, },
}[periodicity]; }[periodicity];
@ -292,13 +295,26 @@ async function getAccounts(rootType) {
return accounts; return accounts;
} }
async function getFiscalYear() { export async function getFiscalYear() {
let { fiscalYearStart, fiscalYearEnd } = await frappe.getSingle( let { fiscalYearStart, fiscalYearEnd } = await frappe.getSingle(
'AccountingSettings' 'AccountingSettings'
); );
//right now quaters received from luxon lib is fixed to Jan as starting quarter
//moving the financial quarters, according to of start of fiscal year month
let quarters = [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4];
let start = DateTime.fromISO(fiscalYearStart);
quarters.unshift(...quarters.splice(13-start.month, 11));
//check if fiscal year ends in next year
let end = DateTime.fromISO(fiscalYearEnd);
let isFiscalSplit = start.year - end.year ;
return { return {
start: fiscalYearStart, start: fiscalYearStart,
end: fiscalYearEnd, end: fiscalYearEnd,
quarters: quarters,
isSplit: isFiscalSplit,
}; };
} }