salix/modules/invoiceIn/back/methods/invoice-in/summary.js

96 lines
3.0 KiB
JavaScript
Raw Normal View History

2021-04-15 08:44:25 +00:00
module.exports = Self => {
Self.remoteMethod('summary', {
description: 'The invoiceIn summary',
accessType: 'READ',
accepts: [{
arg: 'id',
type: 'number',
required: true,
description: 'The invoiceIn id',
http: {source: 'path'}
}],
returns: {
type: 'object',
root: true
},
http: {
path: `/:id/summary`,
verb: 'GET'
}
});
Self.summary = async id => {
const filter = {
include: [
{
relation: 'company',
scope: {
fields: ['id', 'code']
}
},
{
relation: 'supplier',
scope: {
fields: ['id', 'name']
}
2021-04-15 09:54:40 +00:00
},
{
relation: 'sageWithholding',
scope: {
fields: ['withholding']
}
2021-06-14 07:47:04 +00:00
},
{
relation: 'invoiceInTax',
scope: {
fields: ['id', 'invoiceInFk', 'taxableBase', 'expenseFk', 'taxTypeSageFk', 'transactionTypeSageFk', 'foreignValue'],
include: [{
relation: 'transactionTypeSage',
scope: {
fields: ['transaction']
}
},
{
relation: 'taxTypeSage',
scope: {
fields: ['vat']
}
}]
}
},
{
relation: 'expenseDeductible',
scope: {
fields: ['id', 'name', 'taxTypeFk']
}
},
{
relation: 'currency',
scope: {
2021-06-15 07:58:21 +00:00
fields: ['id', 'code']
}
2021-04-15 08:44:25 +00:00
}
]
};
2021-06-25 07:30:24 +00:00
let summaryObj = await Self.app.models.InvoiceIn.findById(id, filter);
summaryObj.totals = await getTotals(id);
return summaryObj;
2021-04-15 08:44:25 +00:00
};
2021-06-25 07:30:24 +00:00
async function getTotals(invoiceInFk) {
return (await Self.rawSql(`
SELECT
SUM(iit.taxableBase) totalTaxableBase,
SUM(iit.taxableBase * (1 + (ti.PorcentajeIva / 100))) totalVat,
SUM(iidd.amount) totalDueDay
FROM vn.invoiceIn ii
LEFT JOIN vn.invoiceInTax iit ON iit.invoiceInFk = ii.id
LEFT JOIN sage.TiposIva ti ON ti.CodigoIva = iit.taxTypeSageFk
LEFT JOIN vn.invoiceInDueDay iidd ON iidd.invoiceInFk = ii.id
WHERE
ii.id = ?`, [invoiceInFk]))[0];
}
2021-04-15 08:44:25 +00:00
};