2020-07-01 12:28:05 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('deleteSales', {
|
|
|
|
description: 'Deletes the selected sales',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'sales',
|
2021-07-29 15:20:57 +00:00
|
|
|
type: ['object'],
|
2020-07-01 12:28:05 +00:00
|
|
|
required: true,
|
|
|
|
description: 'The sales to remove'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'ticketId',
|
2021-07-29 15:20:57 +00:00
|
|
|
type: 'number',
|
2020-07-01 12:28:05 +00:00
|
|
|
required: true,
|
|
|
|
description: 'The ticket id'
|
|
|
|
}],
|
|
|
|
returns: {
|
2021-07-29 15:20:57 +00:00
|
|
|
type: ['object'],
|
2020-07-01 12:28:05 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/deleteSales`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
Self.deleteSales = async(ctx, sales, ticketId, options) => {
|
2021-07-29 15:20:57 +00:00
|
|
|
const $t = ctx.req.__; // $translate
|
2020-07-01 12:28:05 +00:00
|
|
|
const models = Self.app.models;
|
2021-09-29 06:27:18 +00:00
|
|
|
const myOptions = {};
|
|
|
|
let tx;
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
2021-05-26 07:14:18 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-10-04 11:22:11 +00:00
|
|
|
const saleIds = sales.map(sale => sale.id);
|
2022-10-10 11:26:38 +00:00
|
|
|
|
2023-01-31 08:04:45 +00:00
|
|
|
await models.Sale.canEdit(ctx, saleIds, myOptions);
|
2021-09-29 06:27:18 +00:00
|
|
|
|
|
|
|
const ticket = await models.Ticket.findById(ticketId, {
|
|
|
|
include: {
|
|
|
|
relation: 'client',
|
|
|
|
scope: {
|
|
|
|
include: {
|
|
|
|
relation: 'salesPersonUser',
|
|
|
|
scope: {
|
|
|
|
fields: ['id', 'name']
|
|
|
|
}
|
2021-07-29 15:20:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-29 06:27:18 +00:00
|
|
|
}, myOptions);
|
2021-07-29 15:20:57 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const promises = [];
|
|
|
|
let deletions = '';
|
|
|
|
for (let sale of sales) {
|
|
|
|
const deletedSale = models.Sale.destroyById(sale.id, myOptions);
|
|
|
|
deletions += `\r\n-${sale.itemFk}: ${sale.concept} (${sale.quantity})`;
|
2021-07-29 15:20:57 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
promises.push(deletedSale);
|
|
|
|
}
|
2020-07-01 12:28:05 +00:00
|
|
|
|
2024-07-04 10:45:52 +00:00
|
|
|
const deletedSales = await Promise.all(promises);
|
|
|
|
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const salesPerson = ticket.client().salesPersonUser();
|
|
|
|
if (salesPerson) {
|
2023-10-19 06:37:28 +00:00
|
|
|
const url = await Self.app.models.Url.getUrl();
|
2021-07-29 15:20:57 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const message = $t('Deleted sales from ticket', {
|
|
|
|
ticketId: ticketId,
|
2023-10-16 16:24:58 +00:00
|
|
|
ticketUrl: `${url}ticket/${ticketId}/sale`,
|
2021-09-29 06:27:18 +00:00
|
|
|
deletions: deletions
|
|
|
|
});
|
2024-07-04 10:45:52 +00:00
|
|
|
await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message);
|
2021-09-29 06:27:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return deletedSales;
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2020-07-01 12:28:05 +00:00
|
|
|
};
|
|
|
|
};
|