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,
|
2019-07-25 07:55:09 +00:00
|
|
|
description: 'The requested item ID',
|
2019-04-05 13:20:12 +00:00
|
|
|
}, {
|
|
|
|
arg: 'quantity',
|
|
|
|
type: 'Integer',
|
|
|
|
required: true,
|
2019-07-25 07:55:09 +00:00
|
|
|
description: 'The requested item quantity',
|
2019-04-05 13:20:12 +00:00
|
|
|
}],
|
|
|
|
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-07-25 07:55:09 +00:00
|
|
|
let sale;
|
2019-06-12 15:31:45 +00:00
|
|
|
let tx = await Self.beginTransaction({});
|
2019-04-05 13:20:12 +00:00
|
|
|
|
|
|
|
try {
|
2019-06-12 15:31:45 +00:00
|
|
|
let options = {transaction: tx};
|
|
|
|
|
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'}
|
|
|
|
});
|
|
|
|
|
2019-07-02 10:12:15 +00:00
|
|
|
let [[stock]] = await Self.rawSql(`CALL vn.getItemVisibleAvailable(?,?,?,?)`, [
|
2019-04-05 13:20:12 +00:00
|
|
|
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-07-02 10:12:15 +00:00
|
|
|
]);
|
2019-04-05 13:20:12 +00:00
|
|
|
|
2019-07-31 05:46:24 +00:00
|
|
|
if (stock.available < 0)
|
2019-07-02 10:12:15 +00:00
|
|
|
throw new UserError(`This item is not available`);
|
2019-04-05 13:20:12 +00:00
|
|
|
|
2019-07-25 07:55:09 +00:00
|
|
|
|
2019-04-05 13:20:12 +00:00
|
|
|
if (request.saleFk) {
|
2019-07-25 07:55:09 +00:00
|
|
|
sale = await models.Sale.findById(request.saleFk);
|
2019-04-11 07:38:53 +00:00
|
|
|
sale.updateAttributes({
|
|
|
|
itemFk: ctx.args.itemFk,
|
|
|
|
quantity: ctx.args.quantity,
|
2019-07-25 07:55:09 +00:00
|
|
|
concept: item.name,
|
2019-04-11 07:38:53 +00:00
|
|
|
}, options);
|
2019-04-05 13:20:12 +00:00
|
|
|
} else {
|
2019-07-25 07:55:09 +00:00
|
|
|
sale = await models.Sale.create({
|
2019-04-05 13:20:12 +00:00
|
|
|
ticketFk: request.ticketFk,
|
|
|
|
itemFk: ctx.args.itemFk,
|
2019-07-25 07:55:09 +00:00
|
|
|
quantity: ctx.args.quantity,
|
|
|
|
concept: item.name
|
|
|
|
}, options);
|
|
|
|
request.updateAttributes({saleFk: sale.id, itemFk: sale.itemFk, isOk: true}, options);
|
2019-04-05 13:20:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
query = `CALL vn.ticketCalculateSale(?)`;
|
2019-07-25 07:55:09 +00:00
|
|
|
await Self.rawSql(query, [sale.id], options);
|
2019-04-05 13:20:12 +00:00
|
|
|
|
2019-07-25 07:55:09 +00:00
|
|
|
const message = `Se ha comprado ${sale.quantity} unidades de "${sale.concept}" (#${sale.itemFk}) `
|
|
|
|
+ `para el ticket #${sale.ticketFk}`;
|
2019-04-11 07:38:53 +00:00
|
|
|
|
|
|
|
await models.Message.send(ctx, {
|
|
|
|
recipientFk: request.requesterFk,
|
|
|
|
message: message
|
|
|
|
}, options);
|
|
|
|
|
2019-06-12 15:31:45 +00:00
|
|
|
await tx.commit();
|
2019-04-05 13:20:12 +00:00
|
|
|
} catch (error) {
|
2019-06-12 15:31:45 +00:00
|
|
|
await tx.rollback();
|
2019-04-05 13:20:12 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|