2021-12-15 14:24:45 +00:00
|
|
|
module.exports = Self => {
|
2022-07-14 11:40:50 +00:00
|
|
|
Self.remoteMethod('refund', {
|
|
|
|
description: 'Create refund tickets with sales and services if provided',
|
2021-12-15 14:24:45 +00:00
|
|
|
accessType: 'WRITE',
|
2022-07-14 11:40:50 +00:00
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'salesIds',
|
|
|
|
type: ['number'],
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'servicesIds',
|
|
|
|
type: ['number']
|
2023-02-14 09:58:41 +00:00
|
|
|
}
|
2022-07-14 11:40:50 +00:00
|
|
|
],
|
2021-12-15 14:24:45 +00:00
|
|
|
returns: {
|
2022-07-14 11:40:50 +00:00
|
|
|
type: ['number'],
|
2021-12-15 14:24:45 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
2022-04-04 11:26:53 +00:00
|
|
|
path: `/refund`,
|
2021-12-15 14:24:45 +00:00
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-04-21 11:59:28 +00:00
|
|
|
Self.refund = async(salesIds, servicesIds, options) => {
|
2022-07-14 11:40:50 +00:00
|
|
|
const models = Self.app.models;
|
2021-12-15 14:24:45 +00:00
|
|
|
const myOptions = {};
|
|
|
|
let tx;
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-07-14 11:40:50 +00:00
|
|
|
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))];
|
|
|
|
|
2023-02-14 09:58:41 +00:00
|
|
|
const now = Date.vnNew();
|
|
|
|
const [firstTicketId] = ticketsIds;
|
2022-07-14 11:40:50 +00:00
|
|
|
|
2023-04-21 11:59:28 +00:00
|
|
|
const refundTicket = await createTicketRefund(firstTicketId, now, refundAgencyMode, refoundZoneId, myOptions);
|
2023-02-14 09:58:41 +00:00
|
|
|
|
2023-04-21 11:59:28 +00:00
|
|
|
for (const sale of sales) {
|
2022-07-14 11:40:50 +00:00
|
|
|
const createdSale = await models.Sale.create({
|
2023-04-21 11:59:28 +00:00
|
|
|
ticketFk: refundTicket.id,
|
2022-07-14 11:40:50 +00:00
|
|
|
itemFk: sale.itemFk,
|
2022-08-04 12:19:21 +00:00
|
|
|
quantity: - sale.quantity,
|
2022-07-14 11:40:50 +00:00
|
|
|
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) {
|
|
|
|
await models.TicketService.create({
|
|
|
|
description: service.description,
|
2022-08-04 12:19:21 +00:00
|
|
|
quantity: - service.quantity,
|
2022-07-14 11:40:50 +00:00
|
|
|
price: service.price,
|
|
|
|
taxClassFk: service.taxClassFk,
|
2023-04-21 11:59:28 +00:00
|
|
|
ticketFk: refundTicket.id,
|
2022-07-14 11:40:50 +00:00
|
|
|
ticketServiceTypeFk: service.ticketServiceTypeFk,
|
|
|
|
}, myOptions);
|
|
|
|
}
|
|
|
|
}
|
2021-12-21 14:06:57 +00:00
|
|
|
|
2021-12-15 14:24:45 +00:00
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
2023-04-21 11:59:28 +00:00
|
|
|
return refundTicket;
|
2021-12-15 14:24:45 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
2023-02-14 09:58:41 +00:00
|
|
|
|
2023-04-21 11:59:28 +00:00
|
|
|
async function createTicketRefund(ticketId, now, refundAgencyMode, refoundZoneId, myOptions) {
|
2023-02-14 09:58:41 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
await models.TicketRefund.create({
|
|
|
|
refundTicketFk: refundTicket.id,
|
|
|
|
originalTicketFk: ticket.id,
|
|
|
|
}, myOptions);
|
|
|
|
|
2023-04-21 11:59:28 +00:00
|
|
|
return refundTicket;
|
2023-02-14 09:58:41 +00:00
|
|
|
}
|
2021-12-15 14:24:45 +00:00
|
|
|
};
|