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
|
|
|
const myOptions = {};
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
2022-01-20 09:05:24 +00:00
|
|
|
const [result] = await Self.rawSql(`
|
2021-09-28 08:57:51 +00:00
|
|
|
SELECT iit.*,
|
2024-09-02 06:43:53 +00:00
|
|
|
SUM(iidd.amount) totalDueDay,
|
|
|
|
SUM(iidd.foreignValue) totalDueDayForeignValue
|
2021-09-28 08:57:51 +00:00
|
|
|
FROM vn.invoiceIn ii
|
2023-11-22 07:57:45 +00:00
|
|
|
LEFT JOIN (
|
|
|
|
SELECT SUM(iit.taxableBase) totalTaxableBase,
|
2024-09-02 06:43:53 +00:00
|
|
|
SUM(iit.foreignValue) totalTaxableBaseForeignValue,
|
2023-11-22 07:57:45 +00:00
|
|
|
CAST(
|
2023-11-23 10:31:00 +00:00
|
|
|
SUM(IFNULL(iit.taxableBase * (1 + (ti.PorcentajeIva / 100)), iit.taxableBase))
|
2023-11-22 07:57:45 +00:00
|
|
|
AS DECIMAL(10, 2)
|
|
|
|
) totalVat
|
2021-09-28 08:57:51 +00:00
|
|
|
FROM vn.invoiceInTax iit
|
|
|
|
LEFT JOIN sage.TiposIva ti ON ti.CodigoIva = iit.taxTypeSageFk
|
2023-11-22 07:57:45 +00:00
|
|
|
WHERE iit.invoiceInFk = ?
|
|
|
|
) iit ON TRUE
|
2021-09-28 08:57:51 +00:00
|
|
|
LEFT JOIN vn.invoiceInDueDay iidd ON iidd.invoiceInFk = ii.id
|
2023-11-22 07:57:45 +00:00
|
|
|
WHERE ii.id = ?`, [id, id]);
|
2022-01-24 14:35:32 +00:00
|
|
|
return result;
|
2021-09-28 08:57:51 +00:00
|
|
|
};
|
|
|
|
};
|