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',
|
|
|
|
type: 'Integer',
|
|
|
|
required: true,
|
|
|
|
description: 'The request ID',
|
|
|
|
}, {
|
|
|
|
arg: 'itemFk',
|
|
|
|
type: 'Integer',
|
|
|
|
required: true,
|
|
|
|
description: 'The request observation',
|
|
|
|
}, {
|
|
|
|
arg: 'quantity',
|
|
|
|
type: 'Integer',
|
|
|
|
required: true,
|
|
|
|
description: 'The request observation',
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'Object',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:id/confirm`,
|
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.confirm = async ctx => {
|
2019-04-11 07:38:53 +00:00
|
|
|
const models = Self.app.models;
|
2019-04-05 13:20:12 +00:00
|
|
|
let transaction = await Self.beginTransaction({});
|
|
|
|
let options = {transaction: transaction};
|
|
|
|
|
|
|
|
try {
|
2019-04-11 07:38:53 +00:00
|
|
|
let item = await models.Item.findById(ctx.args.itemFk);
|
2019-04-05 13:20:12 +00:00
|
|
|
if (!item)
|
|
|
|
throw new UserError(`That item doesn't exists`);
|
|
|
|
|
2019-04-11 07:38:53 +00:00
|
|
|
let request = await models.TicketRequest.findById(ctx.args.id, {
|
2019-04-05 13:20:12 +00:00
|
|
|
include: {relation: 'ticket'}
|
|
|
|
});
|
|
|
|
|
|
|
|
let query = `CALL vn.getItemVisibleAvailable(?,?,?,?)`;
|
|
|
|
|
|
|
|
let params = [
|
|
|
|
ctx.args.itemFk,
|
|
|
|
request.ticket().shipped,
|
2019-04-18 09:02:28 +00:00
|
|
|
request.ticket().warehouseFk,
|
2019-04-05 13:20:12 +00:00
|
|
|
false
|
|
|
|
];
|
2019-05-21 10:56:29 +00:00
|
|
|
|
2019-04-05 13:20:12 +00:00
|
|
|
let [res] = await Self.rawSql(query, params);
|
|
|
|
let available = res[0].available;
|
|
|
|
if (!available)
|
|
|
|
throw new UserError(`That item is not available on that day`);
|
|
|
|
|
|
|
|
|
|
|
|
if (request.saleFk) {
|
2019-04-11 07:38:53 +00:00
|
|
|
let sale = await models.Sale.findById(request.saleFk);
|
|
|
|
sale.updateAttributes({
|
|
|
|
itemFk: ctx.args.itemFk,
|
|
|
|
quantity: ctx.args.quantity,
|
|
|
|
description: item.description
|
|
|
|
}, options);
|
2019-04-05 13:20:12 +00:00
|
|
|
} else {
|
|
|
|
params = {
|
|
|
|
ticketFk: request.ticketFk,
|
|
|
|
itemFk: ctx.args.itemFk,
|
|
|
|
quantity: ctx.args.quantity
|
|
|
|
};
|
2019-04-11 07:38:53 +00:00
|
|
|
sale = await models.Sale.create(params, options);
|
2019-04-05 13:20:12 +00:00
|
|
|
request.updateAttributes({saleFk: sale.id, itemFk: sale.itemFk}, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
query = `CALL vn.ticketCalculateSale(?)`;
|
|
|
|
params = [sale.id];
|
|
|
|
await Self.rawSql(query, params, options);
|
|
|
|
|
2019-04-11 07:38:53 +00:00
|
|
|
const message = `Se ha comprado ${params.quantity} unidades de "${item.description}" (#${params.itemFk}) `
|
|
|
|
+ `para el ticket #${params.ticketFk}`;
|
|
|
|
|
|
|
|
await models.Message.send(ctx, {
|
|
|
|
recipientFk: request.requesterFk,
|
|
|
|
message: message
|
|
|
|
}, options);
|
|
|
|
|
2019-04-05 13:20:12 +00:00
|
|
|
await transaction.commit();
|
|
|
|
} catch (error) {
|
|
|
|
await transaction.rollback();
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|