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

143 lines
4.7 KiB
JavaScript
Raw Normal View History

module.exports = Self => {
Self.remoteMethod('refund', {
description: 'Create refund tickets with sales and services if provided',
accessType: 'WRITE',
accepts: [
{
arg: 'salesIds',
type: ['number'],
required: true
},
{
arg: 'servicesIds',
type: ['number']
},
],
returns: {
type: ['number'],
root: true
},
http: {
path: `/refund`,
verb: 'post'
}
});
Self.refund = async(salesIds, servicesIds, options) => {
const models = Self.app.models;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const refundAgencyMode = await models.AgencyMode.findOne({
include: {
relation: 'zones',
scope: {
limit: 1,
field: ['id', 'name']
}
},
where: {code: 'refund'}
}, myOptions);
const refoundZoneId = refundAgencyMode.zones()[0].id;
const salesFilter = {
where: {id: {inq: salesIds}},
include: {
relation: 'components',
scope: {
fields: ['saleFk', 'componentFk', 'value']
}
}
};
const sales = await models.Sale.find(salesFilter, myOptions);
const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))];
const refundTickets = [];
const now = new Date();
const mappedTickets = new Map();
for (let ticketId of ticketsIds) {
const filter = {include: {relation: 'address'}};
const ticket = await models.Ticket.findById(ticketId, filter, myOptions);
const refundTicket = await models.Ticket.create({
clientFk: ticket.clientFk,
shipped: now,
addressFk: ticket.address().id,
agencyModeFk: refundAgencyMode.id,
nickname: ticket.address().nickname,
warehouseFk: ticket.warehouseFk,
companyFk: ticket.companyFk,
landed: now,
zoneFk: refoundZoneId
}, myOptions);
refundTickets.push(refundTicket);
mappedTickets.set(ticketId, refundTicket.id);
await models.TicketRefund.create({
refundTicketFk: refundTicket.id,
originalTicketFk: ticket.id,
}, myOptions);
}
for (const sale of sales) {
const refundTicketId = mappedTickets.get(sale.ticketFk);
const createdSale = await models.Sale.create({
ticketFk: refundTicketId,
itemFk: sale.itemFk,
2022-08-04 12:19:21 +00:00
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);
}
if (servicesIds && servicesIds.length > 0) {
const servicesFilter = {
where: {id: {inq: servicesIds}}
};
const services = await models.TicketService.find(servicesFilter, myOptions);
for (const service of services) {
const refundTicketId = mappedTickets.get(service.ticketFk);
await models.TicketService.create({
description: service.description,
2022-08-04 12:19:21 +00:00
quantity: - service.quantity,
price: service.price,
taxClassFk: service.taxClassFk,
ticketFk: refundTicketId,
ticketServiceTypeFk: service.ticketServiceTypeFk,
}, myOptions);
}
}
2021-12-21 14:06:57 +00:00
if (tx) await tx.commit();
return refundTickets;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};