2018-12-21 19:22:13 +00:00
|
|
|
let UserError = require('vn-loopback/common/helpers').UserError;
|
2018-08-02 07:49:00 +00:00
|
|
|
|
2018-06-28 13:24:27 +00:00
|
|
|
module.exports = Self => {
|
2018-07-24 09:27:04 +00:00
|
|
|
Self.remoteMethodCtx('moveToNewTicket', {
|
2018-06-28 13:24:27 +00:00
|
|
|
description: 'Change the state of a ticket',
|
|
|
|
accessType: '',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'ticketParams',
|
|
|
|
type: 'object',
|
|
|
|
required: true,
|
|
|
|
description: '[sales IDs], newTicketFk, actualTicketFk',
|
|
|
|
http: {source: 'body'}
|
|
|
|
}, {
|
|
|
|
arg: 'sales',
|
|
|
|
type: 'object',
|
|
|
|
required: true,
|
|
|
|
description: '[sales IDs]',
|
|
|
|
http: {source: 'body'}
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'string',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/moveToNewTicket`,
|
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-11-27 12:28:15 +00:00
|
|
|
Self.moveToNewTicket = async (ctx, params) => {
|
2018-07-24 09:27:04 +00:00
|
|
|
let userId = ctx.req.accessToken.userId;
|
2018-06-28 13:24:27 +00:00
|
|
|
let model = Self.app.models;
|
|
|
|
let thisTicketIsEditable = await model.Ticket.isEditable(params.ticket.oldTicketFk);
|
|
|
|
if (!thisTicketIsEditable)
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new UserError(`The sales of this ticket can't be modified`);
|
2018-06-28 13:24:27 +00:00
|
|
|
|
|
|
|
let travelDates = await model.Agency.getFirstShipped(params.ticket);
|
2018-07-10 12:55:13 +00:00
|
|
|
let shipped = new Date(travelDates.vShipped);
|
|
|
|
shipped.setMinutes(shipped.getMinutes() + shipped.getTimezoneOffset());
|
|
|
|
|
|
|
|
let landed = new Date(travelDates.vLanded);
|
|
|
|
landed.setMinutes(landed.getMinutes() + landed.getTimezoneOffset());
|
2018-06-28 13:56:45 +00:00
|
|
|
|
2018-06-28 13:24:27 +00:00
|
|
|
let newTicketParams = {
|
|
|
|
clientFk: params.ticket.clientFk,
|
|
|
|
addressFk: params.ticket.addressFk,
|
|
|
|
agencyModeFk: params.ticket.agencyModeFk,
|
|
|
|
warehouseFk: params.ticket.warehouseFk,
|
2018-07-10 12:55:13 +00:00
|
|
|
shipped: shipped,
|
2018-07-24 09:27:04 +00:00
|
|
|
landed: landed,
|
|
|
|
userId: userId
|
2018-06-28 13:24:27 +00:00
|
|
|
};
|
|
|
|
|
2018-10-10 07:59:42 +00:00
|
|
|
let transaction = await Self.beginTransaction({});
|
|
|
|
try {
|
2018-11-27 12:28:15 +00:00
|
|
|
let newTicket = await model.Ticket.new(ctx, newTicketParams, {transaction: transaction});
|
2018-06-28 13:24:27 +00:00
|
|
|
|
2018-10-11 07:41:30 +00:00
|
|
|
let selectedSalesId = [];
|
|
|
|
params.sales.forEach(sale => {
|
|
|
|
selectedSalesId.push(sale.id);
|
|
|
|
});
|
|
|
|
|
2018-10-10 07:59:42 +00:00
|
|
|
await model.Sale.updateAll(
|
2018-10-11 07:41:30 +00:00
|
|
|
{id: {inq: selectedSalesId}},
|
2018-10-10 07:59:42 +00:00
|
|
|
{ticketFk: newTicket.id},
|
2018-10-22 12:35:46 +00:00
|
|
|
{transaction});
|
2018-10-10 07:59:42 +00:00
|
|
|
|
|
|
|
await transaction.commit();
|
2018-07-24 09:27:04 +00:00
|
|
|
|
2018-10-10 07:59:42 +00:00
|
|
|
return newTicket;
|
|
|
|
} catch (e) {
|
|
|
|
await transaction.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2018-06-28 13:24:27 +00:00
|
|
|
};
|
|
|
|
};
|