2023-07-31 08:33:51 +00:00
|
|
|
module.exports = Self => {
|
2024-01-26 07:30:43 +00:00
|
|
|
Self.remoteMethodCtx('clone', {
|
2024-08-23 08:57:35 +00:00
|
|
|
description: 'Clone sales, services, and ticket packaging provided',
|
2024-01-26 07:30:43 +00:00
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [
|
2024-08-23 08:57:35 +00:00
|
|
|
{arg: 'salesIds', type: ['number']},
|
|
|
|
{arg: 'servicesIds', type: ['number']},
|
|
|
|
{arg: 'ticketPackagingIds', type: ['number']},
|
|
|
|
{arg: 'withWarehouse', type: 'boolean', required: true},
|
|
|
|
{arg: 'negative', type: 'boolean'}
|
2024-01-26 07:30:43 +00:00
|
|
|
],
|
2024-08-23 08:57:35 +00:00
|
|
|
returns: {type: ['object'], root: true},
|
|
|
|
http: {path: `/clone`, verb: 'POST'}
|
2024-01-26 07:30:43 +00:00
|
|
|
});
|
2024-08-23 08:57:35 +00:00
|
|
|
|
|
|
|
Self.clone = async(ctx, salesIds, servicesIds, ticketPackagingIds, withWarehouse, negative, options) => {
|
2023-07-31 08:33:51 +00:00
|
|
|
const models = Self.app.models;
|
2023-08-01 14:13:24 +00:00
|
|
|
const myOptions = {};
|
2023-08-01 10:45:57 +00:00
|
|
|
let tx;
|
2023-10-26 07:20:25 +00:00
|
|
|
const newTickets = [];
|
2023-07-28 13:21:43 +00:00
|
|
|
|
2024-08-23 08:57:35 +00:00
|
|
|
if (typeof options === 'object') Object.assign(myOptions, options);
|
2023-08-01 14:13:24 +00:00
|
|
|
|
2023-08-01 10:45:57 +00:00
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
2023-07-28 13:21:43 +00:00
|
|
|
}
|
|
|
|
|
2023-08-01 10:45:57 +00:00
|
|
|
try {
|
2024-01-12 15:18:26 +00:00
|
|
|
let sales;
|
|
|
|
let services;
|
2024-08-23 08:57:35 +00:00
|
|
|
let ticketPackaging;
|
2024-01-12 15:18:26 +00:00
|
|
|
|
2024-08-23 08:57:35 +00:00
|
|
|
if (salesIds?.length) {
|
2024-01-12 15:18:26 +00:00
|
|
|
sales = await models.Sale.find({
|
|
|
|
where: {id: {inq: salesIds}},
|
|
|
|
include: {
|
|
|
|
relation: 'components',
|
|
|
|
scope: {
|
|
|
|
fields: ['saleFk', 'componentFk', 'value']
|
|
|
|
}
|
2023-08-01 10:45:57 +00:00
|
|
|
}
|
2024-01-12 15:18:26 +00:00
|
|
|
}, myOptions);
|
|
|
|
}
|
|
|
|
|
2024-08-23 08:57:35 +00:00
|
|
|
if (servicesIds?.length) {
|
2024-01-12 15:18:26 +00:00
|
|
|
services = await models.TicketService.find({
|
|
|
|
where: {id: {inq: servicesIds}}
|
|
|
|
}, myOptions);
|
|
|
|
}
|
|
|
|
|
2024-08-23 08:57:35 +00:00
|
|
|
if (ticketPackagingIds?.length) {
|
|
|
|
ticketPackaging = await models.TicketPackaging.find({
|
|
|
|
where: {id: {inq: ticketPackagingIds}}
|
|
|
|
}, myOptions);
|
|
|
|
}
|
|
|
|
|
2024-01-12 15:18:26 +00:00
|
|
|
let ticketsIds = sales ?
|
|
|
|
[...new Set(sales.map(sale => sale.ticketFk))] :
|
|
|
|
[...new Set(services.map(service => service.ticketFk))];
|
2023-07-28 13:21:43 +00:00
|
|
|
|
2023-08-24 08:51:34 +00:00
|
|
|
const mappedTickets = new Map();
|
2023-09-22 12:52:56 +00:00
|
|
|
|
2023-09-26 10:42:45 +00:00
|
|
|
for (let ticketId of ticketsIds) {
|
2023-10-26 07:20:25 +00:00
|
|
|
const newTicket = await createTicket(
|
2023-09-22 12:52:56 +00:00
|
|
|
ctx,
|
2023-09-26 10:42:45 +00:00
|
|
|
ticketId,
|
2023-08-24 08:51:34 +00:00
|
|
|
withWarehouse,
|
|
|
|
myOptions
|
|
|
|
);
|
2023-10-26 07:20:25 +00:00
|
|
|
newTickets.push(newTicket);
|
|
|
|
mappedTickets.set(ticketId, newTicket.id);
|
2023-08-01 10:45:57 +00:00
|
|
|
}
|
2024-08-23 08:57:35 +00:00
|
|
|
|
2024-01-12 15:18:26 +00:00
|
|
|
if (sales) {
|
|
|
|
for (const sale of sales) {
|
|
|
|
const newTicketId = mappedTickets.get(sale.ticketFk);
|
2023-08-24 08:51:34 +00:00
|
|
|
|
2024-01-12 15:18:26 +00:00
|
|
|
const createdSale = await models.Sale.create({
|
|
|
|
ticketFk: newTicketId,
|
|
|
|
itemFk: sale.itemFk,
|
|
|
|
quantity: negative ? - sale.quantity : sale.quantity,
|
|
|
|
concept: sale.concept,
|
|
|
|
price: sale.price,
|
|
|
|
discount: sale.discount,
|
|
|
|
}, myOptions);
|
2023-08-24 08:51:34 +00:00
|
|
|
|
2024-01-12 15:18:26 +00:00
|
|
|
const components = sale.components();
|
|
|
|
for (const component of components)
|
|
|
|
component.saleFk = createdSale.id;
|
2023-08-24 08:51:34 +00:00
|
|
|
|
2024-01-12 15:18:26 +00:00
|
|
|
await models.SaleComponent.create(components, myOptions);
|
|
|
|
}
|
2023-08-24 08:51:34 +00:00
|
|
|
}
|
|
|
|
|
2024-01-12 15:18:26 +00:00
|
|
|
if (services) {
|
2023-08-01 10:45:57 +00:00
|
|
|
for (const service of services) {
|
2023-10-26 07:20:25 +00:00
|
|
|
const newTicketId = mappedTickets.get(service.ticketFk);
|
2023-08-24 08:51:34 +00:00
|
|
|
|
2023-08-01 10:45:57 +00:00
|
|
|
await models.TicketService.create({
|
|
|
|
description: service.description,
|
2024-08-23 08:57:35 +00:00
|
|
|
quantity: negative ? -service.quantity : service.quantity,
|
2023-08-01 10:45:57 +00:00
|
|
|
price: service.price,
|
|
|
|
taxClassFk: service.taxClassFk,
|
2023-10-26 07:20:25 +00:00
|
|
|
ticketFk: newTicketId,
|
2023-08-01 10:45:57 +00:00
|
|
|
ticketServiceTypeFk: service.ticketServiceTypeFk,
|
|
|
|
}, myOptions);
|
|
|
|
}
|
|
|
|
}
|
2023-07-28 13:21:43 +00:00
|
|
|
|
2024-08-23 08:57:35 +00:00
|
|
|
if (ticketPackaging) {
|
|
|
|
for (const packaging of ticketPackaging) {
|
|
|
|
const newTicketId = mappedTickets.get(packaging.ticketFk);
|
|
|
|
|
|
|
|
await models.TicketPackaging.create({
|
|
|
|
ticketFk: newTicketId,
|
|
|
|
packagingFk: packaging.packagingFk,
|
|
|
|
quantity: negative ? -packaging.quantity : packaging.quantity
|
|
|
|
}, myOptions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-24 08:51:34 +00:00
|
|
|
if (tx) await tx.commit();
|
2023-07-28 13:21:43 +00:00
|
|
|
|
2023-10-26 07:20:25 +00:00
|
|
|
return newTickets;
|
2023-08-01 10:45:57 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
2023-07-28 13:21:43 +00:00
|
|
|
}
|
2023-08-24 08:51:34 +00:00
|
|
|
|
2024-08-23 08:57:35 +00:00
|
|
|
async function createTicket(ctx, ticketId, withWarehouse, myOptions) {
|
2023-09-26 10:42:45 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
const now = Date.vnNew();
|
2023-08-24 08:51:34 +00:00
|
|
|
|
2023-09-26 10:42:45 +00:00
|
|
|
const ticket = await models.Ticket.findById(ticketId, null, myOptions);
|
2024-05-31 06:44:30 +00:00
|
|
|
if (!ctx.args) ctx.args = {};
|
2023-09-26 10:42:45 +00:00
|
|
|
ctx.args.clientId = ticket.clientFk;
|
|
|
|
ctx.args.shipped = now;
|
|
|
|
ctx.args.landed = now;
|
|
|
|
ctx.args.warehouseId = withWarehouse ? ticket.warehouseFk : null;
|
|
|
|
ctx.args.companyId = ticket.companyFk;
|
|
|
|
ctx.args.addressId = ticket.addressFk;
|
2023-08-24 08:51:34 +00:00
|
|
|
|
2023-10-24 07:34:04 +00:00
|
|
|
const newTicket = await models.Ticket.new(ctx, myOptions);
|
2024-10-01 13:51:30 +00:00
|
|
|
const existingRefund = await models.TicketRefund.findOne({
|
|
|
|
where: {
|
|
|
|
originalTicketFk: ticketId,
|
|
|
|
refundTicketFk: newTicket.id
|
|
|
|
},
|
|
|
|
myOptions
|
|
|
|
});
|
|
|
|
if (!existingRefund) {
|
|
|
|
await models.TicketRefund.create({
|
|
|
|
originalTicketFk: ticketId,
|
|
|
|
refundTicketFk: newTicket.id
|
|
|
|
}, myOptions);
|
|
|
|
}
|
2023-10-24 07:34:04 +00:00
|
|
|
return newTicket;
|
2023-09-26 10:42:45 +00:00
|
|
|
}
|
|
|
|
};
|
2023-07-28 13:21:43 +00:00
|
|
|
};
|