salix/modules/ticket/back/methods/sale/clone.js

117 lines
3.9 KiB
JavaScript
Raw Normal View History

2023-07-31 08:33:51 +00:00
module.exports = Self => {
Self.clone = async(ctx, salesIds, servicesIds, withWarehouse, group, 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 = {};
let tx;
2023-07-28 13:21:43 +00:00
2023-08-01 14:13:24 +00:00
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
2023-07-28 13:21:43 +00:00
}
try {
2023-08-24 08:51:34 +00:00
const salesFilter = {
where: {id: {inq: salesIds}},
include: {
relation: 'components',
scope: {
fields: ['saleFk', 'componentFk', 'value']
}
2023-08-24 08:51:34 +00:00
}
};
const sales = await models.Sale.find(salesFilter, myOptions);
let ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))];
2023-07-28 13:21:43 +00:00
2023-08-24 08:51:34 +00:00
const refundTickets = [];
const mappedTickets = new Map();
const now = Date.vnNew();
if (group) ticketsIds = [ticketsIds[0]];
for (let ticketId in ticketsIds) {
2023-08-24 08:51:34 +00:00
await createTicketRefund(
ctx,
ticketsIds[ticketId],
2023-08-24 08:51:34 +00:00
withWarehouse,
refundTickets,
now,
myOptions
);
mappedTickets.set(ticketsIds[ticketId], refundTickets[ticketId].id);
}
2023-08-24 08:51:34 +00:00
for (const sale of sales) {
2023-09-25 13:23:51 +00:00
const refundTicketId = mappedTickets.get(sale.ticketFk);
2023-08-24 08:51:34 +00:00
const createdSale = await models.Sale.create({
ticketFk: refundTicketId,
itemFk: sale.itemFk,
quantity: negative ? - sale.quantity : sale.quantity,
concept: sale.concept,
price: sale.price,
discount: sale.discount,
}, myOptions);
const components = sale.components();
for (const component of components)
component.saleFk = createdSale.id;
await models.SaleComponent.create(components, myOptions);
}
2023-09-25 13:23:51 +00:00
if (servicesIds && servicesIds.length) {
const servicesFilter = {
where: {id: {inq: servicesIds}}
};
const services = await models.TicketService.find(servicesFilter, myOptions);
2023-08-24 08:51:34 +00:00
for (const service of services) {
2023-09-25 13:23:51 +00:00
const refundTicketId = mappedTickets.get(service.ticketFk);
2023-08-24 08:51:34 +00:00
await models.TicketService.create({
description: service.description,
2023-08-24 08:51:34 +00:00
quantity: negative ? - service.quantity : service.quantity,
price: service.price,
taxClassFk: service.taxClassFk,
2023-08-24 08:51:34 +00:00
ticketFk: refundTicketId,
ticketServiceTypeFk: service.ticketServiceTypeFk,
}, myOptions);
}
}
2023-07-28 13:21:43 +00:00
2023-08-24 08:51:34 +00:00
if (tx) await tx.commit();
2023-07-28 13:21:43 +00:00
2023-08-24 08:51:34 +00:00
return refundTickets;
} catch (e) {
if (tx) await tx.rollback();
throw e;
2023-07-28 13:21:43 +00:00
}
2023-07-31 08:33:51 +00:00
};
2023-08-24 08:51:34 +00:00
async function createTicketRefund(
ctx,
2023-08-24 08:51:34 +00:00
ticketId,
withWarehouse,
refundTickets,
now,
myOptions
) {
const models = Self.app.models;
const ticket = await models.Ticket.findById(ticketId, myOptions);
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
const refundTicket = await models.Ticket.new(ctx, myOptions);
2023-08-24 08:51:34 +00:00
refundTickets.push(refundTicket);
2023-08-24 08:51:34 +00:00
}
2023-07-28 13:21:43 +00:00
};