2022-01-20 20:57:29 +00:00
|
|
|
import frappe from 'frappe';
|
2018-04-29 09:07:59 +00:00
|
|
|
|
2021-11-04 10:31:26 +00:00
|
|
|
export default class AccountsReceivablePayable {
|
2019-08-01 11:52:58 +00:00
|
|
|
async run(reportType, { date }) {
|
|
|
|
const rows = await getReceivablePayable({
|
|
|
|
reportType,
|
2022-01-20 20:57:29 +00:00
|
|
|
date,
|
2019-08-01 11:52:58 +00:00
|
|
|
});
|
2018-04-29 09:07:59 +00:00
|
|
|
|
2019-08-01 11:52:58 +00:00
|
|
|
return { rows };
|
|
|
|
}
|
2022-01-20 20:57:29 +00:00
|
|
|
}
|
2018-04-29 09:07:59 +00:00
|
|
|
|
|
|
|
async function getReceivablePayable({ reportType = 'Receivable', date }) {
|
2019-08-01 11:52:58 +00:00
|
|
|
let entries = [];
|
|
|
|
const debitOrCredit = reportType === 'Receivable' ? 'debit' : 'credit';
|
|
|
|
const referenceType =
|
|
|
|
reportType === 'Receivable' ? 'SalesInvoice' : 'PurchaseInvoice';
|
2018-04-29 09:07:59 +00:00
|
|
|
|
2019-08-01 11:52:58 +00:00
|
|
|
entries = await getLedgerEntries();
|
|
|
|
const vouchers = await getVouchers();
|
2018-04-29 09:07:59 +00:00
|
|
|
|
2019-08-01 11:52:58 +00:00
|
|
|
const futureEntries = getFutureEntries();
|
|
|
|
const returnEntries = getReturnEntries();
|
|
|
|
const pdc = getPDC();
|
2018-04-29 09:07:59 +00:00
|
|
|
|
2019-08-01 11:52:58 +00:00
|
|
|
const validEntries = getValidEntries();
|
2018-04-29 09:07:59 +00:00
|
|
|
|
2019-08-01 11:52:58 +00:00
|
|
|
let data = [];
|
2018-04-29 09:07:59 +00:00
|
|
|
|
2019-08-01 11:52:58 +00:00
|
|
|
for (let entry of validEntries) {
|
|
|
|
const { outStandingAmount, creditNoteAmount } = getOutstandingAmount(entry);
|
2018-04-29 09:07:59 +00:00
|
|
|
|
2019-08-01 11:52:58 +00:00
|
|
|
if (outStandingAmount > 0.1 / 10) {
|
|
|
|
const row = {
|
|
|
|
date: entry.date,
|
2022-01-20 20:57:29 +00:00
|
|
|
party: entry.party,
|
2019-08-01 11:52:58 +00:00
|
|
|
};
|
2018-04-29 09:07:59 +00:00
|
|
|
|
2019-08-01 11:52:58 +00:00
|
|
|
// due date / bill date
|
2018-04-29 09:07:59 +00:00
|
|
|
|
2019-08-01 11:52:58 +00:00
|
|
|
row.voucherType = entry.referenceType;
|
|
|
|
row.voucherNo = entry.referenceName;
|
2018-04-29 09:07:59 +00:00
|
|
|
|
2019-08-01 11:52:58 +00:00
|
|
|
// bill details
|
2018-04-29 09:07:59 +00:00
|
|
|
|
2019-08-01 11:52:58 +00:00
|
|
|
const invoicedAmount = entry[debitOrCredit] || 0;
|
|
|
|
const paidAmount = invoicedAmount - outStandingAmount - creditNoteAmount;
|
2018-04-29 09:07:59 +00:00
|
|
|
|
2019-08-01 11:52:58 +00:00
|
|
|
Object.assign(row, {
|
|
|
|
invoicedAmount,
|
|
|
|
paidAmount,
|
|
|
|
outStandingAmount,
|
2022-01-20 20:57:29 +00:00
|
|
|
creditNoteAmount,
|
2019-08-01 11:52:58 +00:00
|
|
|
});
|
2018-04-29 09:07:59 +00:00
|
|
|
|
2019-08-01 11:52:58 +00:00
|
|
|
// ageing
|
2018-04-29 09:07:59 +00:00
|
|
|
|
2019-08-01 11:52:58 +00:00
|
|
|
data.push(row);
|
2018-04-29 09:07:59 +00:00
|
|
|
}
|
2019-08-01 11:52:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
|
|
|
// helpers
|
|
|
|
|
|
|
|
async function getVouchers() {
|
|
|
|
return await frappe.db.getAll({
|
|
|
|
doctype: referenceType,
|
|
|
|
fields: ['name', 'date'],
|
|
|
|
filters: {
|
2022-01-20 20:57:29 +00:00
|
|
|
submitted: 1,
|
|
|
|
},
|
2019-08-01 11:52:58 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getValidEntries() {
|
2022-01-20 20:57:29 +00:00
|
|
|
return entries.filter((entry) => {
|
2019-08-01 11:52:58 +00:00
|
|
|
return (
|
|
|
|
entry.date <= date &&
|
|
|
|
entry.referenceType === referenceType &&
|
|
|
|
entry[debitOrCredit] > 0
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getOutstandingAmount(entry) {
|
|
|
|
let paymentAmount = 0.0,
|
|
|
|
creditNoteAmount = 0.0;
|
|
|
|
let reverseDebitOrCredit = debitOrCredit === 'debit' ? 'credit' : 'debit';
|
|
|
|
|
|
|
|
for (let e of getEntriesFor(
|
|
|
|
entry.party,
|
|
|
|
entry.referenceType,
|
|
|
|
entry.referenceName
|
|
|
|
)) {
|
|
|
|
if (e.date <= date) {
|
|
|
|
const amount = e[reverseDebitOrCredit] - e[debitOrCredit];
|
|
|
|
|
|
|
|
if (!Object.keys(returnEntries).includes(e.referenceName)) {
|
|
|
|
paymentAmount += amount;
|
|
|
|
} else {
|
|
|
|
creditNoteAmount += amount;
|
2018-04-29 09:07:59 +00:00
|
|
|
}
|
2019-08-01 11:52:58 +00:00
|
|
|
}
|
2018-04-29 09:07:59 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 11:52:58 +00:00
|
|
|
return {
|
|
|
|
outStandingAmount:
|
|
|
|
entry[debitOrCredit] -
|
|
|
|
entry[reverseDebitOrCredit] -
|
|
|
|
paymentAmount -
|
|
|
|
creditNoteAmount,
|
2022-01-20 20:57:29 +00:00
|
|
|
creditNoteAmount,
|
2019-08-01 11:52:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getEntriesFor(party, againstVoucherType, againstVoucher) {
|
|
|
|
// TODO
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFutureEntries() {
|
2022-01-20 20:57:29 +00:00
|
|
|
return entries.filter((entry) => entry.date > date);
|
2019-08-01 11:52:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getReturnEntries() {
|
|
|
|
// TODO
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPDC() {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getLedgerEntries() {
|
|
|
|
if (entries.length) {
|
|
|
|
return entries;
|
2018-04-29 09:07:59 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 11:52:58 +00:00
|
|
|
const partyType = reportType === 'Receivable' ? 'customer' : 'supplier';
|
2022-01-20 20:57:29 +00:00
|
|
|
const partyList = (
|
|
|
|
await frappe.db.getAll({
|
|
|
|
doctype: 'Party',
|
|
|
|
filters: {
|
|
|
|
[partyType]: 1,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
).map((d) => d.name);
|
2019-08-01 11:52:58 +00:00
|
|
|
|
|
|
|
return await frappe.db.getAll({
|
|
|
|
doctype: 'AccountingLedgerEntry',
|
|
|
|
fields: [
|
|
|
|
'name',
|
|
|
|
'date',
|
|
|
|
'account',
|
|
|
|
'party',
|
|
|
|
'referenceType',
|
|
|
|
'referenceName',
|
|
|
|
'sum(debit) as debit',
|
2022-01-20 20:57:29 +00:00
|
|
|
'sum(credit) as credit',
|
2019-08-01 11:52:58 +00:00
|
|
|
],
|
|
|
|
filters: {
|
2022-01-20 20:57:29 +00:00
|
|
|
party: ['in', partyList],
|
2019-08-01 11:52:58 +00:00
|
|
|
},
|
|
|
|
groupBy: ['referenceType', 'referenceName', 'party'],
|
2022-01-20 20:57:29 +00:00
|
|
|
orderBy: 'date',
|
2019-08-01 11:52:58 +00:00
|
|
|
});
|
|
|
|
}
|
2018-04-29 09:07:59 +00:00
|
|
|
}
|