122 lines
4.0 KiB
JavaScript
122 lines
4.0 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = function(Self) {
|
|
Self.remoteMethodCtx('makeInvoice', {
|
|
description: 'Make out an invoice from one or more tickets',
|
|
accessType: 'WRITE',
|
|
accepts: [
|
|
{
|
|
arg: 'ticketsIds',
|
|
description: 'The tickets id',
|
|
type: ['number'],
|
|
required: true
|
|
}
|
|
],
|
|
returns: {
|
|
arg: 'data',
|
|
type: 'boolean',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/makeInvoice`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.makeInvoice = async(ctx, ticketsIds, options) => {
|
|
const userId = ctx.req.accessToken.userId;
|
|
const models = Self.app.models;
|
|
const date = Date.vnNew();
|
|
date.setHours(0, 0, 0, 0);
|
|
|
|
const myOptions = {};
|
|
let tx;
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
let serial;
|
|
let invoiceId;
|
|
try {
|
|
const tickets = await models.Ticket.find({
|
|
where: {
|
|
id: {inq: ticketsIds}
|
|
},
|
|
fields: ['id', 'clientFk', 'companyFk']
|
|
}, myOptions);
|
|
|
|
const [firstTicket] = tickets;
|
|
const clientId = firstTicket.clientFk;
|
|
const companyId = firstTicket.companyFk;
|
|
|
|
const isSameClient = tickets.every(ticket => ticket.clientFk == clientId);
|
|
if (!isSameClient)
|
|
throw new UserError(`You can't invoice tickets from multiple clients`);
|
|
|
|
const clientCanBeInvoiced = await models.Client.canBeInvoiced(clientId, myOptions);
|
|
if (!clientCanBeInvoiced)
|
|
throw new UserError(`This client can't be invoiced`);
|
|
|
|
const ticketCanBeInvoiced = await models.Ticket.canBeInvoiced(ticketsIds, myOptions);
|
|
if (!ticketCanBeInvoiced)
|
|
throw new UserError(`Some of the selected tickets are not billable`);
|
|
|
|
const query = `SELECT vn.invoiceSerial(?, ?, ?) AS serial`;
|
|
const [result] = await Self.rawSql(query, [
|
|
clientId,
|
|
companyId,
|
|
'R'
|
|
], myOptions);
|
|
serial = result.serial;
|
|
|
|
await Self.rawSql(`
|
|
DROP TEMPORARY TABLE IF EXISTS ticketToInvoice;
|
|
CREATE TEMPORARY TABLE ticketToInvoice
|
|
(PRIMARY KEY (id))
|
|
ENGINE = MEMORY
|
|
SELECT id FROM vn.ticket
|
|
WHERE id IN(?) AND refFk IS NULL
|
|
`, [ticketsIds], myOptions);
|
|
|
|
await Self.rawSql('CALL invoiceOut_new(?, ?, null, @invoiceId)', [serial, date], myOptions);
|
|
|
|
const [resultInvoice] = await Self.rawSql('SELECT @invoiceId id', [], myOptions);
|
|
|
|
invoiceId = resultInvoice.id;
|
|
|
|
for (let ticket of tickets) {
|
|
const ticketInvoice = await models.Ticket.findById(ticket.id, {
|
|
fields: ['refFk']
|
|
}, myOptions);
|
|
|
|
await models.TicketLog.create({
|
|
originFk: ticket.id,
|
|
userFk: userId,
|
|
action: 'insert',
|
|
changedModel: 'Ticket',
|
|
changedModelId: ticket.id,
|
|
newInstance: ticketInvoice
|
|
}, myOptions);
|
|
}
|
|
|
|
if (serial != 'R' && invoiceId)
|
|
await Self.rawSql('CALL invoiceOutBooking(?)', [invoiceId], myOptions);
|
|
|
|
if (tx) await tx.commit();
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
|
|
if (serial != 'R' && invoiceId)
|
|
await models.InvoiceOut.createPdf(ctx, invoiceId);
|
|
|
|
return {invoiceFk: invoiceId, serial: serial};
|
|
};
|
|
};
|