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 = {};
|
2023-08-01 10:45:57 +00:00
|
|
|
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);
|
|
|
|
|
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
|
|
|
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}},
|
2023-08-01 10:45:57 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
};
|
|
|
|
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);
|
2023-08-01 10:45:57 +00:00
|
|
|
const saleComponentsFilter = {
|
|
|
|
where: {saleFk: {inq: saleIds}},
|
|
|
|
scope: {
|
|
|
|
fields: ['saleFk', 'componentFk', 'value']
|
|
|
|
}
|
|
|
|
};
|
2023-08-01 14:13:24 +00:00
|
|
|
for (const sale of salesByTicket) {
|
2023-08-01 10:45:57 +00:00
|
|
|
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
|
2023-08-01 10:45:57 +00:00
|
|
|
// const components = sale.components();
|
|
|
|
for (const component of components)
|
|
|
|
component.saleFk = createdSale.id;
|
2023-07-28 13:21:43 +00:00
|
|
|
|
2023-08-01 10:45:57 +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
|
|
|
|
2023-08-01 10:45:57 +00:00
|
|
|
const query = `CALL vn.ticket_recalc(?, NULL)`;
|
2023-07-28 13:21:43 +00:00
|
|
|
|
2023-08-01 10:45:57 +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;
|
2023-08-01 10:45:57 +00:00
|
|
|
} else {
|
|
|
|
for (const updatedTicket of updatedTickets)
|
|
|
|
await Self.rawSql(query, [updatedTicket.id], myOptions);
|
|
|
|
if (tx) await tx.commit();
|
2023-08-04 06:22:43 +00:00
|
|
|
return /* updatedTickets */updatedTickets;
|
2023-07-31 08:33:51 +00:00
|
|
|
}
|
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-07-31 08:33:51 +00:00
|
|
|
};
|
2023-07-28 13:21:43 +00:00
|
|
|
};
|