2022-01-20 20:57:29 +00:00
|
|
|
import { unique } from 'frappe/utils';
|
2021-11-04 10:31:26 +00:00
|
|
|
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,
|
2022-01-20 20:57:29 +00:00
|
|
|
periodicity,
|
2019-12-03 11:45:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let expense = await getData({
|
|
|
|
rootType: 'Expense',
|
|
|
|
balanceMustBe: 'Debit',
|
|
|
|
fromDate,
|
|
|
|
toDate,
|
2022-01-20 20:57:29 +00:00
|
|
|
periodicity,
|
2019-12-03 11:45:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let incomeTotalRow = income.totalRow;
|
|
|
|
incomeTotalRow.account = {
|
2022-01-20 20:57:29 +00:00
|
|
|
template: `<span class="font-semibold">${income.totalRow.account}</span>`,
|
2019-12-03 11:45:07 +00:00
|
|
|
};
|
2018-04-24 07:58:57 +00:00
|
|
|
|
2019-12-03 11:45:07 +00:00
|
|
|
let expenseTotalRow = expense.totalRow;
|
|
|
|
expenseTotalRow.account = {
|
2022-01-20 20:57:29 +00:00
|
|
|
template: `<span class="font-semibold">${expense.totalRow.account}</span>`,
|
2019-12-03 11:45:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let rows = [
|
|
|
|
...income.accounts,
|
|
|
|
incomeTotalRow,
|
|
|
|
{
|
|
|
|
account: {
|
2022-01-20 20:57:29 +00:00
|
|
|
template: '<span> </span>',
|
2019-12-04 17:24:52 +00:00
|
|
|
},
|
2022-01-20 20:57:29 +00:00
|
|
|
isGroup: 1,
|
2019-12-03 11:45:07 +00:00
|
|
|
},
|
|
|
|
...expense.accounts,
|
|
|
|
expenseTotalRow,
|
|
|
|
{
|
|
|
|
account: {
|
2022-01-20 20:57:29 +00:00
|
|
|
template: '<span> </span>',
|
2019-12-04 17:24:52 +00:00
|
|
|
},
|
2022-01-20 20:57:29 +00:00
|
|
|
isGroup: 1,
|
|
|
|
},
|
2019-12-03 11:45:07 +00:00
|
|
|
];
|
|
|
|
|
2022-01-20 20:57:29 +00:00
|
|
|
rows = rows.map((row) => {
|
2019-12-03 11:45:07 +00:00
|
|
|
if (row.indent === 0) {
|
|
|
|
row.account = {
|
2022-01-20 20:57:29 +00:00
|
|
|
template: `<span class="font-semibold">${row.account}</span>`,
|
2019-12-03 11:45:07 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
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 = {
|
2022-01-20 20:57:29 +00:00
|
|
|
account: 'Total Profit',
|
2019-12-03 11:45:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (let column of columns) {
|
|
|
|
profitRow[column] =
|
|
|
|
(income.totalRow[column] || 0.0) - (expense.totalRow[column] || 0.0);
|
|
|
|
|
2022-01-20 20:57:29 +00:00
|
|
|
rows.forEach((row) => {
|
2019-12-03 11:45:07 +00:00
|
|
|
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;
|