2018-04-26 10:23:27 +00:00
|
|
|
const frappe = require('frappejs');
|
|
|
|
|
|
|
|
class PurchaseRegister {
|
2019-08-01 11:52:58 +00:00
|
|
|
async run({ fromDate, toDate, supplier }) {
|
2019-08-14 07:43:49 +00:00
|
|
|
if (!fromDate && !toDate && !supplier) return { rows: [] };
|
|
|
|
|
2019-08-01 11:52:58 +00:00
|
|
|
let filters = {};
|
|
|
|
if (supplier) {
|
|
|
|
filters.supplier = supplier;
|
2018-04-26 10:23:27 +00:00
|
|
|
}
|
2019-08-01 11:52:58 +00:00
|
|
|
|
|
|
|
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 };
|
|
|
|
}
|
2018-04-26 10:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PurchaseRegister;
|