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

68 lines
2.6 KiB
JavaScript
Raw Normal View History

2023-07-31 08:33:51 +00:00
module.exports = Self => {
Self.clone = async(sales, refundAgencyMode, refoundZoneId, servicesIds, withWarehouse, isRefund, myOptions) => {
const models = Self.app.models;
const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))];
2023-07-28 13:21:43 +00:00
2023-07-31 08:33:51 +00:00
const now = Date.vnNew();
let updatedTickets = [];
2023-07-28 13:21:43 +00:00
for (const ticketId of ticketsIds) {
2023-07-31 08:33:51 +00:00
const filter = {include: {relation: 'address'}};
const ticket = await models.Ticket.findById(ticketId, filter, myOptions);
const ticketUpdated = await models.Ticket.create({
clientFk: ticket.clientFk,
shipped: now,
addressFk: ticket.address().id,
agencyModeFk: refundAgencyMode.id,
nickname: ticket.address().nickname,
warehouseFk: withWarehouse ? ticket.warehouseFk : null,
companyFk: ticket.companyFk,
landed: now,
zoneFk: refoundZoneId
}, myOptions);
updatedTickets.push(ticketUpdated);
2023-07-28 13:21:43 +00:00
}
2023-07-31 08:33:51 +00:00
for (const sale of sales) {
const createdSale = await models.Sale.create({
ticketFk: sale.ticketFk,
itemFk: sale.itemFk,
quantity: (isRefund) ? - sale.quantity : sale.quantity,
concept: sale.concept,
price: sale.price,
discount: sale.discount,
}, myOptions);
2023-07-28 13:21:43 +00:00
2023-07-31 08:33:51 +00:00
const components = sale.components();
for (const component of components)
component.saleFk = createdSale.id;
2023-07-28 13:21:43 +00:00
2023-07-31 08:33:51 +00:00
await models.SaleComponent.create(components, myOptions);
}
2023-07-28 13:21:43 +00:00
2023-07-31 08:33:51 +00:00
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: (isRefund) ? - 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-07-31 08:33:51 +00:00
const query = `CALL vn.ticket_recalc(?, NULL)`;
for (const updatedTicket of updatedTickets)
await Self.rawSql(query, [updatedTicket.id], myOptions);
return updatedTickets.map(updatedTicket => updatedTicket.id);
};
2023-07-28 13:21:43 +00:00
};