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

164 lines
5.6 KiB
JavaScript
Raw Normal View History

module.exports = Self => {
2023-06-01 06:32:06 +00:00
Self.remoteMethodCtx('refund', {
description: 'Create refund tickets with sales and services if provided',
accessType: 'WRITE',
accepts: [
{
arg: 'salesIds',
2023-06-07 12:12:04 +00:00
type: ['number']
},
{
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
}
],
returns: {
type: ['number'],
root: true
},
http: {
path: `/refund`,
verb: 'post'
}
});
2023-06-19 06:46:15 +00:00
Self.refund = async(ctx, salesIds, servicesIds, withWarehouse, options) => {
const models = Self.app.models;
2023-06-01 06:32:06 +00:00
const myOptions = {userId: ctx.req.accessToken.userId};
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;
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;
2023-06-07 12:12:04 +00:00
if (salesIds) {
const salesFilter = {
where: {id: {inq: salesIds}},
include: {
relation: 'components',
scope: {
fields: ['saleFk', 'componentFk', 'value']
}
}
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);
}
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);
}
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-06-12 10:42:30 +00:00
quantity: service.quantity,
price: - service.price,
taxClassFk: service.taxClassFk,
ticketFk: refundTicket.id,
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);
if (tx) await tx.commit();
return refundTicket;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
2023-06-19 06:46:15 +00:00
async function createTicketRefund(ticketId, now, refundAgencyMode, refoundZoneId, withWarehouse, myOptions) {
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,
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);
return refundTicket;
}
};