2021-12-15 14:24:45 +00:00
|
|
|
module.exports = Self => {
|
2023-06-01 06:32:06 +00:00
|
|
|
Self.remoteMethodCtx('refund', {
|
2022-07-14 11:40:50 +00:00
|
|
|
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',
|
2023-06-07 12:12:04 +00:00
|
|
|
type: ['number']
|
2022-07-14 11:40:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'servicesIds',
|
|
|
|
type: ['number']
|
2023-06-19 06:46:15 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'withWarehouse',
|
|
|
|
type: 'boolean',
|
|
|
|
required: true
|
2023-05-03 12:12:26 +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-06-19 06:46:15 +00:00
|
|
|
Self.refund = async(ctx, salesIds, servicesIds, withWarehouse, options) => {
|
2022-07-14 11:40:50 +00:00
|
|
|
const models = Self.app.models;
|
2023-06-01 06:32:06 +00:00
|
|
|
const myOptions = {userId: ctx.req.accessToken.userId};
|
2021-12-15 14:24:45 +00:00
|
|
|
let tx;
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
|
2023-06-07 12:12:04 +00:00
|
|
|
let refundTicket = null;
|
2021-12-15 14:24:45 +00:00
|
|
|
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;
|
|
|
|
|
2023-06-07 12:12:04 +00:00
|
|
|
if (salesIds) {
|
|
|
|
const salesFilter = {
|
|
|
|
where: {id: {inq: salesIds}},
|
|
|
|
include: {
|
|
|
|
relation: 'components',
|
|
|
|
scope: {
|
|
|
|
fields: ['saleFk', 'componentFk', 'value']
|
|
|
|
}
|
2022-07-14 11:40:50 +00:00
|
|
|
}
|
2023-06-07 12:12:04 +00:00
|
|
|
};
|
|
|
|
const sales = await models.Sale.find(salesFilter, myOptions);
|
|
|
|
const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))];
|
|
|
|
|
|
|
|
const now = Date.vnNew();
|
|
|
|
const [firstTicketId] = ticketsIds;
|
|
|
|
|
2023-06-09 12:30:46 +00:00
|
|
|
// eslint-disable-next-line max-len
|
2023-06-19 06:46:15 +00:00
|
|
|
refundTicket = await createTicketRefund(firstTicketId, now, refundAgencyMode, refoundZoneId, withWarehouse, myOptions);
|
2023-06-07 12:12:04 +00:00
|
|
|
|
|
|
|
for (const sale of sales) {
|
|
|
|
const createdSale = await models.Sale.create({
|
|
|
|
ticketFk: refundTicket.id,
|
|
|
|
itemFk: sale.itemFk,
|
|
|
|
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);
|
2022-07-14 11:40:50 +00:00
|
|
|
}
|
2023-06-07 12:12:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!refundTicket) {
|
|
|
|
const servicesFilter = {
|
|
|
|
where: {id: {inq: servicesIds}}
|
|
|
|
};
|
|
|
|
const services = await models.TicketService.find(servicesFilter, myOptions);
|
|
|
|
const ticketsIds = [...new Set(services.map(service => service.ticketFk))];
|
|
|
|
|
|
|
|
const now = Date.vnNew();
|
|
|
|
const [firstTicketId] = ticketsIds;
|
|
|
|
|
2023-06-09 12:30:46 +00:00
|
|
|
// eslint-disable-next-line max-len
|
2023-07-31 09:06:19 +00:00
|
|
|
refundTicket = await createTicketRefund(firstTicketId, now, refundAgencyMode, refoundZoneId, withWarehouse, myOptions);
|
2022-07-14 11:40:50 +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,
|
2023-10-06 04:16:42 +00:00
|
|
|
quantity: - service.quantity,
|
|
|
|
price: service.price,
|
2022-07-14 11:40:50 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2023-05-30 07:18:52 +00:00
|
|
|
|
2023-06-29 08:39:07 +00:00
|
|
|
const query = `CALL vn.ticket_recalc(?, NULL)`;
|
2023-05-30 07:18:52 +00:00
|
|
|
await Self.rawSql(query, [refundTicket.id], myOptions);
|
|
|
|
|
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-06-19 06:46:15 +00:00
|
|
|
async function createTicketRefund(ticketId, now, refundAgencyMode, refoundZoneId, withWarehouse, 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,
|
2023-06-19 06:46:15 +00:00
|
|
|
warehouseFk: withWarehouse ? ticket.warehouseFk : null,
|
2023-02-14 09:58:41 +00:00
|
|
|
companyFk: ticket.companyFk,
|
|
|
|
landed: now,
|
|
|
|
zoneFk: refoundZoneId
|
|
|
|
}, myOptions);
|
|
|
|
|
|
|
|
await models.TicketRefund.create({
|
|
|
|
refundTicketFk: refundTicket.id,
|
|
|
|
originalTicketFk: ticket.id,
|
2023-05-03 11:03:14 +00:00
|
|
|
}, myOptions);
|
2023-02-14 09:58:41 +00:00
|
|
|
|
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
|
|
|
};
|