2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00
books/reports/PurchaseRegister/PurchaseRegister.js
18alantom 1a8a23d2a2 build(electron): bump electron to 15.3.0
- refactor Common Js imports to ES6
2021-11-05 14:31:35 +05:30

57 lines
1.3 KiB
JavaScript

import frappe from 'frappejs';
class PurchaseRegister {
async run({ fromDate, toDate, supplier }) {
if (!fromDate && !toDate && !supplier) return { rows: [] };
let filters = {};
if (supplier) {
filters.supplier = supplier;
}
if (fromDate && toDate) {
filters.date = ['>=', fromDate, '<=', toDate];
} else if (fromDate) {
filters.date = ['>=', fromDate];
} else if (toDate) {
filters.date = ['<=', toDate];
}
filters.submitted = 1;
const bills = await frappe.db.getAll({
doctype: 'PurchaseInvoice',
fields: ['name', 'date', 'supplier', 'account', 'netTotal', 'grandTotal'],
filters,
orderBy: 'date',
order: 'desc'
});
const billNames = bills.map(d => d.name);
const taxes = await frappe.db.getAll({
doctype: 'TaxSummary',
fields: ['parent', 'amount'],
filters: {
parenttype: 'PurchaseInvoice',
parent: ['in', billNames]
},
orderBy: 'name'
});
for (let bill of bills) {
bill.totalTax = taxes
.filter(tax => tax.parent === bill.name)
.reduce((acc, tax) => {
if (tax.amount) {
acc = acc + tax.amount;
}
return acc;
}, 0);
}
return { rows: bills };
}
}
export default PurchaseRegister;