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-09-20 09:06:30 +00:00
|
|
|
type: ['number'],
|
|
|
|
required: true
|
2022-07-14 11:40:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'servicesIds',
|
|
|
|
type: ['number']
|
2023-05-23 13:06:38 +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: {
|
2023-11-20 06:56:15 +00:00
|
|
|
type: ['object'],
|
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-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 {
|
2023-09-21 07:14:46 +00:00
|
|
|
const refundsTicket = await models.Sale.clone(
|
2023-09-22 12:52:56 +00:00
|
|
|
ctx,
|
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-01 10:45:57 +00:00
|
|
|
true,
|
2023-07-28 13:21:43 +00:00
|
|
|
myOptions
|
|
|
|
);
|
2023-08-01 10:45:57 +00:00
|
|
|
|
2023-09-21 14:08:55 +00:00
|
|
|
if (tx) await tx.commit();
|
2023-07-28 13:21:43 +00:00
|
|
|
|
2023-11-20 06:56:15 +00:00
|
|
|
return refundsTicket;
|
2021-12-15 14:24:45 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|