2018-06-28 13:24:27 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('moveToNewTicket', {
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.moveToNewTicket = async params => {
|
|
|
|
let model = Self.app.models;
|
|
|
|
let thisTicketIsEditable = await model.Ticket.isEditable(params.ticket.oldTicketFk);
|
|
|
|
if (!thisTicketIsEditable)
|
|
|
|
throw new Error(`The sales of this ticket can't be modified`);
|
|
|
|
|
|
|
|
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,
|
|
|
|
landed: landed
|
2018-06-28 13:24:27 +00:00
|
|
|
};
|
|
|
|
|
2018-07-11 11:52:04 +00:00
|
|
|
let newTicket = await model.Ticket.new(newTicketParams);
|
2018-06-28 13:24:27 +00:00
|
|
|
|
|
|
|
for (let i = 0; i < params.sales.length; i++) {
|
2018-07-11 11:52:04 +00:00
|
|
|
await model.Sale.update({id: params.sales[i].id}, {ticketFk: newTicket});
|
2018-06-28 13:24:27 +00:00
|
|
|
}
|
|
|
|
return newTicket;
|
|
|
|
};
|
|
|
|
};
|