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

110 lines
3.4 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'
}
});
2021-06-28 08:15:54 +00:00
Self.summary = async(id, options) => {
let myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
2021-04-15 08:44:25 +00:00
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: {
2021-06-30 06:26:03 +00:00
fields: [
'id',
'invoiceInFk',
'taxableBase',
'expenseFk',
'taxTypeSageFk',
'transactionTypeSageFk',
'foreignValue'],
2021-06-14 07:47:04 +00:00
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
}
]
};
let summaryObj = await Self.app.models.InvoiceIn.findById(id, filter, myOptions);
2021-07-03 07:19:03 +00:00
2021-06-25 07:30:24 +00:00
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(`
2021-06-29 08:33:01 +00:00
SELECT iit.*,
2021-06-25 07:30:24 +00:00
SUM(iidd.amount) totalDueDay
FROM vn.invoiceIn ii
2021-06-29 08:33:01 +00:00
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
2021-06-25 07:30:24 +00:00
LEFT JOIN vn.invoiceInDueDay iidd ON iidd.invoiceInFk = ii.id
WHERE
2021-06-29 08:33:01 +00:00
ii.id = ?`, [invoiceInFk, invoiceInFk]))[0];
2021-06-25 07:30:24 +00:00
}
2021-04-15 08:44:25 +00:00
};