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

103 lines
4.2 KiB
JavaScript
Raw Normal View History

2023-07-31 08:33:51 +00:00
module.exports = Self => {
2023-08-03 12:01:50 +00:00
Self.clone = async(sales, 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
}
const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))];
const now = Date.vnNew();
let updatedTickets = [];
let newTicket;
const filter = {
include: [
{relation: 'address'},
{
relation: 'sale',
2023-08-03 12:01:50 +00:00
where: {saleFk: {inq: sales}},
},
]
};
try {
for (const [index, ticketId] of ticketsIds.entries()) {
const ticket = await models.Ticket.findById(ticketId, filter, myOptions);
if (!group || !index) {
newTicket = await models.Ticket.create({
clientFk: ticket.clientFk,
shipped: now,
addressFk: ticket.address().id,
nickname: ticket.address().nickname,
warehouseFk: withWarehouse ? ticket.warehouseFk : null,
companyFk: ticket.companyFk,
landed: now,
}, myOptions);
updatedTickets.push(newTicket);
}
2023-08-01 14:13:24 +00:00
const salesByTicket = ticket.sale();
const saleIds = salesByTicket.map(sale => sale.id);
const saleComponentsFilter = {
where: {saleFk: {inq: saleIds}},
scope: {
fields: ['saleFk', 'componentFk', 'value']
}
};
2023-08-01 14:13:24 +00:00
for (const sale of salesByTicket) {
const createdSale = await models.Sale.create({
ticketFk: newTicket.id,
itemFk: sale.itemFk,
quantity: (negative) ? - sale.quantity : sale.quantity,
concept: sale.concept,
price: sale.price,
discount: sale.discount,
}, myOptions);
2023-08-01 14:13:24 +00:00
const components = await models.SaleComponent.find(saleComponentsFilter, myOptions); // Revisar con Alex
// const components = sale.components();
for (const component of components)
component.saleFk = createdSale.id;
2023-07-28 13:21:43 +00:00
await models.SaleComponent.create(components, myOptions);
}
}
if (servicesIds && servicesIds.length > 0) {
const servicesFilter = {
where: {id: {inq: servicesIds}}
};
const services = await models.TicketService.find(servicesFilter, myOptions);
for (const service of services) {
await models.TicketService.create({
description: service.description,
quantity: (negative) ? - service.quantity : service.quantity,
price: service.price,
taxClassFk: service.taxClassFk,
ticketFk: service.ticketFk,
ticketServiceTypeFk: service.ticketServiceTypeFk,
}, myOptions);
}
}
2023-07-28 13:21:43 +00:00
const query = `CALL vn.ticket_recalc(?, NULL)`;
2023-07-28 13:21:43 +00:00
if (group) {
await Self.rawSql(query, [newTicket.id], myOptions);
if (tx) await tx.commit();
2023-08-01 14:13:24 +00:00
return newTicket;
} else {
for (const updatedTicket of updatedTickets)
await Self.rawSql(query, [updatedTicket.id], myOptions);
if (tx) await tx.commit();
2023-08-03 12:01:50 +00:00
return /* updatedTickets */updatedTickets.map(updatedTicket => updatedTicket.id);
2023-07-31 08:33:51 +00:00
}
} 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-07-28 13:21:43 +00:00
};