salix/modules/invoiceOut/back/methods/invoiceOut/transferInvoice.js

108 lines
3.5 KiB
JavaScript
Raw Normal View History

const UserError = require('vn-loopback/util/user-error');
2023-07-28 13:21:43 +00:00
module.exports = Self => {
2023-08-24 08:51:34 +00:00
Self.remoteMethodCtx('transferInvoice', {
description: 'Transfer an issued invoice to another client',
2023-07-28 13:21:43 +00:00
accessType: 'WRITE',
accepts: [
{
arg: 'id',
type: 'number',
required: true,
description: 'Issued invoice id'
},
{
2023-11-20 06:56:15 +00:00
arg: 'refFk',
type: 'string',
required: true
},
{
arg: 'newClientFk',
type: 'number',
required: true
},
{
arg: 'cplusRectificationTypeFk',
type: 'number',
required: true
},
{
2023-11-20 06:56:15 +00:00
arg: 'siiTypeInvoiceOutFk',
type: 'number',
required: true
},
{
2023-11-20 06:56:15 +00:00
arg: 'invoiceCorrectionTypeFk',
type: 'number',
required: true
},
2023-07-28 13:21:43 +00:00
],
returns: {
type: 'boolean',
root: true
},
http: {
path: '/transferInvoice',
verb: 'post'
}
});
Self.transferInvoice = async(ctx, options) => {
2023-07-28 13:21:43 +00:00
const models = Self.app.models;
const myOptions = {userId: ctx.req.accessToken.userId};
const {id, refFk, newClientFk, cplusRectificationTypeFk, siiTypeInvoiceOutFk, invoiceCorrectionTypeFk} = ctx.args;
2023-07-28 13:21:43 +00:00
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
2023-11-20 06:56:15 +00:00
const {clientFk} = await models.InvoiceOut.findById(id);
2023-11-20 06:56:15 +00:00
if (clientFk == newClientFk)
throw new UserError(`Select a different client`);
2023-07-28 13:21:43 +00:00
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
2023-11-20 06:56:15 +00:00
const filterRef = {where: {refFk: refFk}};
2023-08-24 08:51:34 +00:00
const tickets = await models.Ticket.find(filterRef, myOptions);
2023-07-28 13:21:43 +00:00
const ticketsIds = tickets.map(ticket => ticket.id);
2023-11-20 06:56:15 +00:00
const refundTickets = await models.Ticket.refund(ctx, ticketsIds, null, myOptions);
2023-08-24 08:51:34 +00:00
const filterTicket = {where: {ticketFk: {inq: ticketsIds}}};
const services = await models.TicketService.find(filterTicket, myOptions);
2023-07-28 13:21:43 +00:00
const servicesIds = services.map(service => service.id);
2023-08-24 08:51:34 +00:00
const sales = await models.Sale.find(filterTicket, myOptions);
const salesIds = sales.map(sale => sale.id);
const clonedTickets = await models.Sale.clone(ctx, salesIds, servicesIds, null, false, myOptions);
const clonedTicketIds = [];
2023-08-03 12:01:50 +00:00
for (const clonedTicket of clonedTickets) {
2023-11-20 06:56:15 +00:00
await clonedTicket.updateAttribute('clientFk', newClientFk, myOptions);
clonedTicketIds.push(clonedTicket.id);
2023-08-03 12:01:50 +00:00
}
2023-08-01 14:13:24 +00:00
const invoiceCorrection = {
correctedFk: id,
cplusRectificationTypeFk,
siiTypeInvoiceOutFk,
invoiceCorrectionTypeFk
};
2023-11-20 06:56:15 +00:00
const refundTicketIds = refundTickets.map(ticket => ticket.id);
2023-07-28 13:21:43 +00:00
2023-11-20 06:56:15 +00:00
await models.Ticket.invoiceTickets(ctx, refundTicketIds, invoiceCorrection, myOptions);
2023-08-03 12:01:50 +00:00
const [invoiceId] = await models.Ticket.invoiceTicketsAndPdf(ctx, clonedTicketIds, null, myOptions);
2023-08-24 08:51:34 +00:00
2023-08-03 12:01:50 +00:00
return invoiceId;
2023-07-28 13:21:43 +00:00
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};