2023-09-21 07:14:46 +00:00
|
|
|
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: [
|
|
|
|
{
|
2023-08-01 10:45:57 +00:00
|
|
|
arg: 'id',
|
|
|
|
type: 'number',
|
2023-09-21 07:14:46 +00:00
|
|
|
required: true,
|
|
|
|
description: 'Issued invoice id'
|
2023-08-01 10:45:57 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'ref',
|
|
|
|
type: 'string',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'newClientFk',
|
|
|
|
type: 'number',
|
2023-09-22 12:52:56 +00:00
|
|
|
required: true
|
2023-08-01 10:45:57 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'cplusRectificationId',
|
|
|
|
type: 'number',
|
2023-09-22 12:52:56 +00:00
|
|
|
required: true
|
2023-08-01 10:45:57 +00:00
|
|
|
},
|
|
|
|
{
|
2023-11-14 14:45:52 +00:00
|
|
|
arg: 'siiTypeInvoiceOutId',
|
2023-08-01 10:45:57 +00:00
|
|
|
type: 'number',
|
2023-09-22 12:52:56 +00:00
|
|
|
required: true
|
2023-08-01 10:45:57 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'invoiceCorrectionTypeId',
|
|
|
|
type: 'number',
|
2023-09-22 12:52:56 +00:00
|
|
|
required: true
|
2023-08-01 10:45:57 +00:00
|
|
|
},
|
2023-07-28 13:21:43 +00:00
|
|
|
],
|
|
|
|
returns: {
|
|
|
|
type: 'boolean',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: '/transferInvoice',
|
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-09-21 07:14:46 +00:00
|
|
|
Self.transferInvoice = async(ctx, options) => {
|
2023-07-28 13:21:43 +00:00
|
|
|
const models = Self.app.models;
|
2023-08-04 06:22:43 +00:00
|
|
|
const myOptions = {userId: ctx.req.accessToken.userId};
|
2023-09-21 07:14:46 +00:00
|
|
|
const args = ctx.args;
|
2023-07-28 13:21:43 +00:00
|
|
|
let tx;
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
2023-09-21 07:14:46 +00:00
|
|
|
const {clientFk} = await models.InvoiceOut.findById(args.id);
|
|
|
|
|
|
|
|
if (clientFk == args.newClientFk)
|
2023-09-22 12:52:56 +00:00
|
|
|
throw new UserError(`Select a different client`);
|
2023-09-21 07:14:46 +00:00
|
|
|
|
2023-07-28 13:21:43 +00:00
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
try {
|
2023-09-21 07:14:46 +00:00
|
|
|
const filterRef = {where: {refFk: args.ref}};
|
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-07-31 08:33:51 +00:00
|
|
|
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);
|
|
|
|
|
2023-09-22 12:52:56 +00:00
|
|
|
const clonedTickets = await models.Sale.clone(ctx, salesIds, servicesIds, null, false, false, myOptions);
|
2023-08-04 06:22:43 +00:00
|
|
|
const clonedTicketIds = [];
|
|
|
|
|
2023-08-03 12:01:50 +00:00
|
|
|
for (const clonedTicket of clonedTickets) {
|
2023-09-21 07:14:46 +00:00
|
|
|
await clonedTicket.updateAttribute('clientFk', args.newClientFk, myOptions);
|
2023-08-04 06:22:43 +00:00
|
|
|
clonedTicketIds.push(clonedTicket.id);
|
2023-08-03 12:01:50 +00:00
|
|
|
}
|
2023-08-01 14:13:24 +00:00
|
|
|
|
2023-08-24 08:51:34 +00:00
|
|
|
const invoiceIds = await models.Ticket.invoiceTickets(ctx, clonedTicketIds, myOptions);
|
|
|
|
const [invoiceId] = invoiceIds;
|
2023-07-28 13:21:43 +00:00
|
|
|
|
2023-08-03 12:01:50 +00:00
|
|
|
await models.InvoiceCorrection.create({
|
|
|
|
correctingFk: invoiceId,
|
2023-09-21 07:14:46 +00:00
|
|
|
correctedFk: args.id,
|
|
|
|
cplusRectificationTypeFk: args.cplusRectificationId,
|
2023-11-14 14:45:52 +00:00
|
|
|
siiTypeInvoiceOutFk: args.siiTypeInvoiceOutId,
|
2023-09-22 12:52:56 +00:00
|
|
|
invoiceCorrectionTypeFk: args.invoiceCorrectionTypeId
|
2023-08-04 06:22:43 +00:00
|
|
|
}, myOptions);
|
2023-08-03 12:01:50 +00:00
|
|
|
|
2023-09-25 10:54:31 +00:00
|
|
|
if (tx) {
|
|
|
|
await tx.commit();
|
|
|
|
await models.InvoiceOut.makePdfAndNotify(ctx, invoiceId, null);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|