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

49 lines
1.7 KiB
JavaScript

module.exports = Self => {
Self.remoteMethod('getTotals', {
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'
}
});
Self.getTotals = async(id, options) => {
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const [result] = await Self.rawSql(`
SELECT iit.*,
SUM(iidd.amount) totalDueDay,
SUM(iidd.foreignValue) totalDueDayForeignValue
FROM vn.invoiceIn ii
LEFT JOIN (
SELECT SUM(iit.taxableBase) totalTaxableBase,
SUM(iit.foreignValue) totalTaxableBaseForeignValue,
CAST(
SUM(IFNULL(iit.taxableBase * (1 + (ti.PorcentajeIva / 100)), iit.taxableBase))
AS DECIMAL(10, 2)
) 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]);
return result;
};
};