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

114 lines
3.3 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',
type: ['number'],
required: true
},
{
arg: 'servicesIds',
type: ['number']
},
{
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-07-28 13:21:43 +00:00
Self.refund = async(ctx, salesIds, servicesIds, withWarehouse, options) => {
const models = Self.app.models;
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;
}
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']
2023-07-28 13:21:43 +00:00
}
}
};
2023-08-24 08:51:34 +00:00
// const sales = await models.Sale.find(salesFilter, myOptions);
2023-08-01 14:13:24 +00:00
const refundTicket = await models.Sale.clone(
2023-08-24 08:51:34 +00:00
salesIds,
2023-07-31 08:33:51 +00:00
servicesIds,
2023-07-28 13:21:43 +00:00
withWarehouse,
2023-08-24 08:51:34 +00:00
// refundAgencyMode,
// refoundZoneId,
true,
true,
2023-07-28 13:21:43 +00:00
myOptions
);
2023-08-24 08:51:34 +00:00
if (tx && !options) await tx.commit();
2023-07-28 13:21:43 +00:00
return refundTicket;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
/* 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,
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;
} */
};