module.exports = Self => { Self.remoteMethod('summary', { description: 'The invoiceOut summary', accessType: 'READ', accepts: [{ arg: 'id', type: 'number', required: true, description: 'The invoiceOut id', http: {source: 'path'} }], returns: { type: 'object', root: true }, http: { path: `/:id/summary`, verb: 'GET' } }); Self.summary = async(id, options) => { let summary = {}; const myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options); const filter = { fields: [ 'id', 'ref', 'issued', 'dued', 'amount', 'created', 'booked', 'clientFk', 'companyFk', 'hasPdf' ], where: {id: id}, include: [ { relation: 'company', scope: { fields: ['id', 'code'] } }, { relation: 'supplier', scope: { fields: ['id', 'countryFk'] } }, { relation: 'client', scope: { fields: ['id', 'socialName', 'email'] } } ] }; summary.invoiceOut = await Self.app.models.InvoiceOut.findOne(filter, myOptions); const invoiceOutTaxes = await Self.rawSql(` SELECT iot.* , pgc.*, IF(pe.equFk IS NULL, taxableBase, 0) AS Base, pgc.rate / 100 as vatPercent FROM vn.invoiceOutTax iot JOIN vn.pgc ON pgc.code = iot.pgcFk LEFT JOIN vn.pgcEqu pe ON pe.equFk = pgc.code WHERE invoiceOutFk = ?`, [summary.invoiceOut.id], myOptions); summary.invoiceOut.taxesBreakdown = invoiceOutTaxes; return summary; }; };