2019-04-05 13:20:12 +00:00
|
|
|
let UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('confirm', {
|
|
|
|
description: '',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'id',
|
2021-10-07 07:57:07 +00:00
|
|
|
type: 'number',
|
2019-04-05 13:20:12 +00:00
|
|
|
required: true,
|
|
|
|
description: 'The request ID',
|
|
|
|
}, {
|
|
|
|
arg: 'itemFk',
|
2021-10-07 07:57:07 +00:00
|
|
|
type: 'number',
|
2019-04-05 13:20:12 +00:00
|
|
|
required: true,
|
2019-07-25 07:55:09 +00:00
|
|
|
description: 'The requested item ID',
|
2019-04-05 13:20:12 +00:00
|
|
|
}, {
|
|
|
|
arg: 'quantity',
|
2021-10-07 07:57:07 +00:00
|
|
|
type: 'number',
|
2019-04-05 13:20:12 +00:00
|
|
|
required: true,
|
2019-07-25 07:55:09 +00:00
|
|
|
description: 'The requested item quantity',
|
2019-04-05 13:20:12 +00:00
|
|
|
}],
|
|
|
|
returns: {
|
2021-10-07 07:57:07 +00:00
|
|
|
type: 'object',
|
2019-04-05 13:20:12 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:id/confirm`,
|
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-10-07 07:57:07 +00:00
|
|
|
Self.confirm = async(ctx, options) => {
|
2020-05-27 06:07:14 +00:00
|
|
|
const userId = ctx.req.accessToken.userId;
|
2019-04-11 07:38:53 +00:00
|
|
|
const models = Self.app.models;
|
2020-01-23 12:31:07 +00:00
|
|
|
const $t = ctx.req.__; // $translate
|
2021-10-07 07:57:07 +00:00
|
|
|
const myOptions = {};
|
|
|
|
let tx;
|
2019-04-05 13:20:12 +00:00
|
|
|
|
2021-10-07 07:57:07 +00:00
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
2019-06-12 15:31:45 +00:00
|
|
|
|
2021-10-07 07:57:07 +00:00
|
|
|
try {
|
|
|
|
const item = await models.Item.findById(ctx.args.itemFk, null, myOptions);
|
2019-04-05 13:20:12 +00:00
|
|
|
if (!item)
|
|
|
|
throw new UserError(`That item doesn't exists`);
|
|
|
|
|
2021-10-07 07:57:07 +00:00
|
|
|
const request = await models.TicketRequest.findById(ctx.args.id, {
|
2019-04-05 13:20:12 +00:00
|
|
|
include: {relation: 'ticket'}
|
2021-10-07 07:57:07 +00:00
|
|
|
}, myOptions);
|
|
|
|
|
|
|
|
const itemStock = await models.Item.getVisibleAvailable(
|
|
|
|
ctx.args.itemFk,
|
|
|
|
request.ticket().warehouseFk,
|
|
|
|
request.ticket().shipped,
|
|
|
|
myOptions
|
|
|
|
);
|
2019-04-05 13:20:12 +00:00
|
|
|
|
2020-10-02 07:39:58 +00:00
|
|
|
const isAvailable = itemStock.available > 0;
|
2019-04-05 13:20:12 +00:00
|
|
|
|
2020-10-02 07:39:58 +00:00
|
|
|
if (!isAvailable)
|
2019-07-02 10:12:15 +00:00
|
|
|
throw new UserError(`This item is not available`);
|
2019-04-05 13:20:12 +00:00
|
|
|
|
2020-10-02 07:39:58 +00:00
|
|
|
if (request.saleFk)
|
|
|
|
throw new UserError(`This request already contains a sale`);
|
|
|
|
|
2021-10-07 07:57:07 +00:00
|
|
|
const sale = await models.Sale.create({
|
2020-10-02 07:39:58 +00:00
|
|
|
ticketFk: request.ticketFk,
|
|
|
|
itemFk: ctx.args.itemFk,
|
|
|
|
quantity: ctx.args.quantity,
|
|
|
|
concept: item.name
|
2021-10-07 07:57:07 +00:00
|
|
|
}, myOptions);
|
2020-10-02 07:39:58 +00:00
|
|
|
await request.updateAttributes({
|
|
|
|
saleFk: sale.id,
|
|
|
|
itemFk: sale.itemFk,
|
|
|
|
isOk: true
|
2021-10-07 07:57:07 +00:00
|
|
|
}, myOptions);
|
2019-04-05 13:20:12 +00:00
|
|
|
|
2021-10-07 07:57:07 +00:00
|
|
|
const query = `CALL vn.sale_calculateComponent(?, NULL)`;
|
|
|
|
await Self.rawSql(query, [sale.id], myOptions);
|
2019-04-05 13:20:12 +00:00
|
|
|
|
2020-01-23 12:31:07 +00:00
|
|
|
const origin = ctx.req.headers.origin;
|
|
|
|
const requesterId = request.requesterFk;
|
2021-10-07 07:57:07 +00:00
|
|
|
|
2021-03-12 11:24:08 +00:00
|
|
|
const message = $t('Bought units from buy request', {
|
2020-01-23 12:31:07 +00:00
|
|
|
quantity: sale.quantity,
|
|
|
|
concept: sale.concept,
|
|
|
|
itemId: sale.itemFk,
|
|
|
|
ticketId: sale.ticketFk,
|
2021-03-12 11:24:08 +00:00
|
|
|
url: `${origin}/#!/ticket/${sale.ticketFk}/summary`,
|
|
|
|
urlItem: `${origin}/#!/item/${sale.itemFk}/summary`
|
2020-01-23 12:31:07 +00:00
|
|
|
});
|
2021-10-07 07:57:07 +00:00
|
|
|
await models.Chat.sendCheckingPresence(ctx, requesterId, message, myOptions);
|
2019-04-11 07:38:53 +00:00
|
|
|
|
2021-10-07 07:57:07 +00:00
|
|
|
const logRecord = {
|
2020-05-27 06:07:14 +00:00
|
|
|
originFk: sale.ticketFk,
|
|
|
|
userFk: userId,
|
|
|
|
action: 'update',
|
|
|
|
changedModel: 'ticketRequest',
|
|
|
|
newInstance: {
|
|
|
|
destinationFk: sale.ticketFk,
|
2020-06-02 09:15:07 +00:00
|
|
|
quantity: sale.quantity,
|
|
|
|
concept: sale.concept,
|
|
|
|
itemId: sale.itemFk,
|
|
|
|
ticketId: sale.ticketFk,
|
2020-05-27 06:07:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-07 07:57:07 +00:00
|
|
|
await Self.app.models.TicketLog.create(logRecord, myOptions);
|
2020-05-27 06:07:14 +00:00
|
|
|
|
2021-10-07 07:57:07 +00:00
|
|
|
if (tx) await tx.commit();
|
2019-10-22 11:44:36 +00:00
|
|
|
|
|
|
|
return sale;
|
2021-10-07 07:57:07 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
2019-04-05 13:20:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|