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

110 lines
3.4 KiB
JavaScript

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, options) => {
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const filter = {
include: [
{
relation: 'company',
scope: {
fields: ['id', 'code']
}
},
{
relation: 'supplier',
scope: {
fields: ['id', 'name']
}
},
{
relation: 'sageWithholding',
scope: {
fields: ['withholding']
}
},
{
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: {
fields: ['id', 'code']
}
}
]
};
let summaryObj = await Self.app.models.InvoiceIn.findById(id, filter, myOptions);
summaryObj.totals = await getTotals(id);
return summaryObj;
};
async function getTotals(invoiceInFk) {
return (await Self.rawSql(`
SELECT iit.*,
SUM(iidd.amount) totalDueDay
FROM vn.invoiceIn ii
LEFT JOIN (SELECT SUM(iit.taxableBase) totalTaxableBase,
SUM(iit.taxableBase * (1 + (ti.PorcentajeIva / 100))) totalVat
FROM vn.invoiceInTax iit
LEFT JOIN sage.TiposIva ti ON ti.CodigoIva = iit.taxTypeSageFk
WHERE iit.invoiceInFk = ?) iit ON TRUE
LEFT JOIN vn.invoiceInDueDay iidd ON iidd.invoiceInFk = ii.id
WHERE
ii.id = ?`, [invoiceInFk, invoiceInFk]))[0];
}
};