2018-04-26 10:23:27 +00:00
|
|
|
const frappe = require('frappejs');
|
|
|
|
|
|
|
|
class SalesRegister {
|
2018-09-07 10:10:39 +00:00
|
|
|
async run({ fromDate, toDate, customer }) {
|
|
|
|
let filters = {};
|
|
|
|
if(customer) {
|
|
|
|
filters.customer = customer;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fromDate && toDate) {
|
|
|
|
filters.date = ['>=', fromDate, '<=', toDate];
|
|
|
|
}
|
|
|
|
else if (fromDate) {
|
|
|
|
filters.date = ['>=', fromDate];
|
|
|
|
}
|
|
|
|
else if (toDate) {
|
|
|
|
filters.date = ['<=', toDate];
|
|
|
|
}
|
|
|
|
|
2018-04-26 10:23:27 +00:00
|
|
|
const invoices = await frappe.db.getAll({
|
|
|
|
doctype: 'Invoice',
|
|
|
|
fields: ['name', 'date', 'customer', 'account', 'netTotal', 'grandTotal'],
|
2018-09-07 10:10:39 +00:00
|
|
|
filters: filters,
|
2018-04-26 10:23:27 +00:00
|
|
|
orderBy: 'date',
|
|
|
|
order: 'desc'
|
|
|
|
});
|
|
|
|
|
|
|
|
const invoiceNames = invoices.map(d => d.name);
|
|
|
|
|
|
|
|
const taxes = await frappe.db.getAll({
|
|
|
|
doctype: 'TaxSummary',
|
|
|
|
fields: ['parent', 'amount'],
|
|
|
|
filters: {
|
|
|
|
parenttype: 'Invoice',
|
|
|
|
parent: ['in', invoiceNames]
|
|
|
|
},
|
|
|
|
orderBy: 'name'
|
|
|
|
});
|
|
|
|
|
|
|
|
for (let invoice of invoices) {
|
|
|
|
invoice.totalTax = taxes
|
|
|
|
.filter(tax => tax.parent === invoice.name)
|
|
|
|
.reduce((acc, tax) => {
|
|
|
|
if (tax.amount) {
|
|
|
|
acc = acc + tax.amount;
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return { rows: invoices };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SalesRegister;
|