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

57 lines
1.6 KiB
JavaScript
Raw Normal View History

2021-09-24 06:24:34 +00:00
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('toBook', {
description: 'To book the invoiceIn',
accessType: 'WRITE',
accepts: {
arg: 'id',
type: 'number',
required: true,
description: 'The invoiceIn id',
http: {source: 'path'}
},
returns: {
type: 'object',
root: true
},
http: {
path: '/:id/toBook',
verb: 'POST'
}
});
Self.toBook = async(ctx, id, options) => {
const models = Self.app.models;
let tx;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const totals = await models.InvoiceIn.getTotals(id);
if (totals.totalDueDay != totals.totalTaxableBase && totals.totalDueDay != totals.totalVal)
throw new UserError(`Amounts do not match`);
/* const now = new Date();
const hasFuturePayments = await models.InvoiceInDueDay.findOne({
where: {
invoiceInFk: id,
dueDated: {gte: now}
}
});*/
await Self.rawSql(`CALL vn.invoiceInBookingMain(?)`, [id], myOptions);
if (tx) await tx.commit();
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};