2021-11-04 10:31:26 +00:00
|
|
|
import frappe from 'frappejs';
|
|
|
|
import { unique } from 'frappejs/utils';
|
|
|
|
import { getData } from '../FinancialStatements/FinancialStatements';
|
2018-04-18 09:02:05 +00:00
|
|
|
|
|
|
|
class ProfitAndLoss {
|
2019-12-03 11:45:07 +00:00
|
|
|
async run({ fromDate, toDate, periodicity }) {
|
|
|
|
let income = await getData({
|
|
|
|
rootType: 'Income',
|
|
|
|
balanceMustBe: 'Credit',
|
|
|
|
fromDate,
|
|
|
|
toDate,
|
|
|
|
periodicity
|
|
|
|
});
|
|
|
|
|
|
|
|
let expense = await getData({
|
|
|
|
rootType: 'Expense',
|
|
|
|
balanceMustBe: 'Debit',
|
|
|
|
fromDate,
|
|
|
|
toDate,
|
|
|
|
periodicity
|
|
|
|
});
|
|
|
|
|
|
|
|
let incomeTotalRow = income.totalRow;
|
|
|
|
incomeTotalRow.account = {
|
|
|
|
template: `<span class="font-semibold">${income.totalRow.account}</span>`
|
|
|
|
};
|
2018-04-24 07:58:57 +00:00
|
|
|
|
2019-12-03 11:45:07 +00:00
|
|
|
let expenseTotalRow = expense.totalRow;
|
|
|
|
expenseTotalRow.account = {
|
|
|
|
template: `<span class="font-semibold">${expense.totalRow.account}</span>`
|
|
|
|
};
|
|
|
|
|
|
|
|
let rows = [
|
|
|
|
...income.accounts,
|
|
|
|
incomeTotalRow,
|
|
|
|
{
|
|
|
|
account: {
|
|
|
|
template: '<span> </span>'
|
2019-12-04 17:24:52 +00:00
|
|
|
},
|
|
|
|
isGroup: 1
|
2019-12-03 11:45:07 +00:00
|
|
|
},
|
|
|
|
...expense.accounts,
|
|
|
|
expenseTotalRow,
|
|
|
|
{
|
|
|
|
account: {
|
|
|
|
template: '<span> </span>'
|
2019-12-04 17:24:52 +00:00
|
|
|
},
|
|
|
|
isGroup: 1
|
2019-12-03 11:45:07 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
rows = rows.map(row => {
|
|
|
|
if (row.indent === 0) {
|
|
|
|
row.account = {
|
|
|
|
template: `<span class="font-semibold">${row.account}</span>`
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return row;
|
|
|
|
});
|
2018-04-24 07:58:57 +00:00
|
|
|
|
2019-12-03 11:45:07 +00:00
|
|
|
const columns = unique([...income.periodList, ...expense.periodList]);
|
2018-04-24 07:58:57 +00:00
|
|
|
|
2019-12-03 11:45:07 +00:00
|
|
|
let profitRow = {
|
|
|
|
account: 'Total Profit'
|
|
|
|
};
|
|
|
|
|
|
|
|
for (let column of columns) {
|
|
|
|
profitRow[column] =
|
|
|
|
(income.totalRow[column] || 0.0) - (expense.totalRow[column] || 0.0);
|
|
|
|
|
|
|
|
rows.forEach(row => {
|
|
|
|
if (!row.isGroup) {
|
|
|
|
row[column] = row[column] || 0.0;
|
|
|
|
}
|
|
|
|
});
|
2018-04-18 09:02:05 +00:00
|
|
|
}
|
2019-12-03 11:45:07 +00:00
|
|
|
|
|
|
|
rows.push(profitRow);
|
|
|
|
|
|
|
|
return { rows, columns };
|
|
|
|
}
|
2018-04-18 09:02:05 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 10:31:26 +00:00
|
|
|
export default ProfitAndLoss;
|