2018-04-18 09:02:05 +00:00
|
|
|
const frappe = require('frappejs');
|
2018-04-24 07:58:57 +00:00
|
|
|
const { unique } = require('frappejs/utils');
|
|
|
|
const { getData } = require('../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>'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
...expense.accounts,
|
|
|
|
expenseTotalRow,
|
|
|
|
{
|
|
|
|
account: {
|
|
|
|
template: '<span> </span>'
|
2018-04-24 07:58:57 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2018-04-26 10:23:27 +00:00
|
|
|
module.exports = ProfitAndLoss;
|