2020-07-01 12:28:05 +00:00
|
|
|
let UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-10-04 11:22:11 +00:00
|
|
|
const canEditSales = await models.Sale.canEdit(ctx, saleIds, myOptions);
|
2022-10-10 11:26:38 +00:00
|
|
|
if (!canEditSales)
|
|
|
|
throw new UserError(`Sale(s) blocked, please contact production`);
|
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
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
const salesPerson = ticket.client().salesPersonUser();
|
|
|
|
if (salesPerson) {
|
|
|
|
const origin = ctx.req.headers.origin;
|
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,
|
|
|
|
ticketUrl: `${origin}/#!/ticket/${ticketId}/sale`,
|
|
|
|
deletions: deletions
|
|
|
|
});
|
|
|
|
await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message, myOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
const deletedSales = await Promise.all(promises);
|
2021-07-29 15:20:57 +00:00
|
|
|
|
2021-09-29 06:27:18 +00:00
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
|
|
|
return deletedSales;
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2020-07-01 12:28:05 +00:00
|
|
|
};
|
|
|
|
};
|