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;
|
|
|
|
let transaction = await Self.beginTransaction({});
|
|
|
|
let options = {transaction};
|
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);
|
|
|
|
let shipped = new Date(travelDates.vShipped);
|
|
|
|
shipped.setMinutes(shipped.getMinutes() + shipped.getTimezoneOffset());
|
|
|
|
|
|
|
|
let landed = new Date(travelDates.vLanded);
|
|
|
|
landed.setMinutes(landed.getMinutes() + landed.getTimezoneOffset());
|
|
|
|
|
|
|
|
newTicketData = {
|
|
|
|
clientFk: params.currentTicket.clientFk,
|
|
|
|
addressFk: params.currentTicket.addressFk,
|
|
|
|
agencyModeFk: params.currentTicket.agencyModeFk,
|
|
|
|
warehouseFk: params.currentTicket.warehouseFk,
|
|
|
|
shipped: shipped,
|
|
|
|
landed: landed,
|
|
|
|
userId: userId
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
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);
|
|
|
|
await transaction.commit();
|
|
|
|
|
|
|
|
return receiverTicket;
|
|
|
|
} catch (error) {
|
|
|
|
await transaction.rollback();
|
|
|
|
throw error;
|
|
|
|
}
|
2018-06-06 14:54:40 +00:00
|
|
|
};
|
|
|
|
};
|