2018-12-27 11:54:16 +00:00
|
|
|
let UserError = require('vn-loopback/util/user-error');
|
2018-08-02 07:49:00 +00:00
|
|
|
|
2018-06-06 14:54:40 +00:00
|
|
|
module.exports = Self => {
|
2019-05-24 13:30:50 +00:00
|
|
|
Self.remoteMethodCtx('moveToTicket', {
|
|
|
|
description: 'Moves lines to a new or a given ticket',
|
2018-06-06 14:54:40 +00:00
|
|
|
accepts: [{
|
|
|
|
arg: 'params',
|
|
|
|
type: 'object',
|
|
|
|
required: true,
|
2019-05-24 13:30:50 +00:00
|
|
|
description: 'currentTicket, receiverTicket, [sales IDs], removeEmptyTicket',
|
2018-06-06 14:54:40 +00:00
|
|
|
http: {source: 'body'}
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'string',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/moveToTicket`,
|
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-05-24 13:30:50 +00:00
|
|
|
Self.moveToTicket = async(ctx, params) => {
|
|
|
|
const models = Self.app.models;
|
|
|
|
const userId = ctx.req.accessToken.userId;
|
|
|
|
let currentTicket = await models.Ticket.findById(params.currentTicket.currentTicketId);
|
|
|
|
let newTicketData = {};
|
|
|
|
let receiverTicket = params.receiverTicket;
|
2018-06-06 14:54:40 +00:00
|
|
|
|
2019-05-24 13:30:50 +00:00
|
|
|
let isCurrentTicketEditable = await models.Ticket.isEditable(params.currentTicket.currentTicketId);
|
|
|
|
if (!isCurrentTicketEditable)
|
|
|
|
throw new UserError(`The sales of the current ticket can't be modified`);
|
2018-06-06 14:54:40 +00:00
|
|
|
|
2019-05-24 13:30:50 +00:00
|
|
|
if (params.receiverTicket.id) {
|
|
|
|
let isReceiverTicketEditable = await models.Ticket.isEditable(params.receiverTicket.id);
|
|
|
|
if (!isReceiverTicketEditable)
|
|
|
|
throw new UserError(`The sales of the receiver ticket can't be modified`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!params.receiverTicket.id) {
|
|
|
|
let travelDates = await models.Agency.getFirstShipped(params.currentTicket);
|
2019-06-25 09:17:58 +00:00
|
|
|
|
|
|
|
if (!travelDates)
|
|
|
|
throw new UserError(`Invalid parameters to create a new ticket`);
|
2019-06-07 11:07:23 +00:00
|
|
|
let shipped = new Date(travelDates.shipped);
|
|
|
|
let landed = new Date(travelDates.landed);
|
2019-05-24 13:30:50 +00:00
|
|
|
|
|
|
|
newTicketData = {
|
|
|
|
clientFk: params.currentTicket.clientFk,
|
|
|
|
addressFk: params.currentTicket.addressFk,
|
|
|
|
agencyModeFk: params.currentTicket.agencyModeFk,
|
|
|
|
warehouseFk: params.currentTicket.warehouseFk,
|
|
|
|
shipped: shipped,
|
|
|
|
landed: landed,
|
|
|
|
userId: userId
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-13 07:21:36 +00:00
|
|
|
let tx = await Self.beginTransaction({});
|
|
|
|
|
2019-05-24 13:30:50 +00:00
|
|
|
try {
|
2019-06-13 07:21:36 +00:00
|
|
|
let options = {transaction: tx};
|
|
|
|
|
2019-05-24 13:30:50 +00:00
|
|
|
if (!params.receiverTicket.id)
|
|
|
|
receiverTicket = await models.Ticket.new(ctx, newTicketData, options);
|
|
|
|
|
|
|
|
let promises = [];
|
|
|
|
for (let sale of params.sales) {
|
|
|
|
promises.push(
|
|
|
|
models.Sale.update(
|
|
|
|
{id: sale.id},
|
|
|
|
{ticketFk: receiverTicket.id},
|
|
|
|
options
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (params.removeEmptyTicket)
|
|
|
|
promises.push(currentTicket.updateAttributes({isDeleted: true}, options));
|
|
|
|
|
|
|
|
await Promise.all(promises);
|
2019-06-13 07:21:36 +00:00
|
|
|
await tx.commit();
|
2019-05-24 13:30:50 +00:00
|
|
|
|
|
|
|
return receiverTicket;
|
|
|
|
} catch (error) {
|
2019-06-13 07:21:36 +00:00
|
|
|
await tx.rollback();
|
2019-05-24 13:30:50 +00:00
|
|
|
throw error;
|
|
|
|
}
|
2018-06-06 14:54:40 +00:00
|
|
|
};
|
|
|
|
};
|