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

53 lines
1.6 KiB
JavaScript
Raw Normal View History

2021-09-28 08:57:51 +00:00
module.exports = Self => {
2021-10-05 09:19:48 +00:00
Self.remoteMethod('getTotals', {
2021-09-28 08:57:51 +00:00
description: 'Return totals for an invoiceIn',
accessType: 'READ',
accepts: {
arg: 'id',
type: 'number',
required: true,
description: 'invoiceIn id',
http: {source: 'path'}
},
returns: {
type: 'object',
root: true
},
http: {
path: '/:id/getTotals',
verb: 'GET'
}
});
2021-10-05 09:19:48 +00:00
Self.getTotals = async(id, options) => {
2021-09-28 08:57:51 +00:00
let tx;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
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 = ?`, [id, id]))[0];
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};