2018-09-26 09:16:52 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('importToNewRefundTicket', {
|
|
|
|
description: 'Imports lines from claimBeginning to a new ticket with specific shipped, landed dates, agency and company',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'id',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
description: 'the claim id',
|
|
|
|
http: {source: 'path'}
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'string',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:id/importToNewRefundTicket`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-10 07:59:42 +00:00
|
|
|
async function addSalesToTicket(salesToRefund, ticketFk, transaction) {
|
2018-10-02 07:41:30 +00:00
|
|
|
let formatedSales = [];
|
2018-09-26 09:16:52 +00:00
|
|
|
salesToRefund.forEach(sale => {
|
|
|
|
let formatedSale = {
|
|
|
|
itemFk: sale.sale().itemFk,
|
2018-10-10 07:59:42 +00:00
|
|
|
ticketFk: ticketFk,
|
2018-09-26 09:16:52 +00:00
|
|
|
concept: sale.sale().concept,
|
|
|
|
quantity: -Math.abs(sale.quantity),
|
|
|
|
price: sale.sale().price,
|
|
|
|
discount: sale.sale().discount,
|
|
|
|
reserved: sale.sale().reserved,
|
|
|
|
isPicked: sale.sale().isPicked,
|
|
|
|
created: sale.sale().created
|
|
|
|
};
|
2018-10-02 07:41:30 +00:00
|
|
|
formatedSales.push(formatedSale);
|
2018-09-26 09:16:52 +00:00
|
|
|
});
|
2018-10-02 07:41:30 +00:00
|
|
|
return await Self.app.models.Sale.create(formatedSales, transaction);
|
2018-09-26 09:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function insertIntoClaimEnd(createdSales, claimId, workerId, transaction) {
|
2018-10-02 07:41:30 +00:00
|
|
|
let formatedSales = [];
|
2018-09-26 09:16:52 +00:00
|
|
|
createdSales.forEach(sale => {
|
|
|
|
let formatedSale = {
|
|
|
|
saleFk: sale.id,
|
|
|
|
claimFk: claimId,
|
|
|
|
workerFk: workerId
|
|
|
|
};
|
2018-10-02 07:41:30 +00:00
|
|
|
formatedSales.push(formatedSale);
|
2018-09-26 09:16:52 +00:00
|
|
|
});
|
2018-10-02 07:41:30 +00:00
|
|
|
return await Self.app.models.ClaimEnd.create(formatedSales, transaction);
|
2018-09-26 09:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function saveObservation(observation, transaction) {
|
|
|
|
let query = `INSERT INTO vn.ticketObservation(ticketFk, observationTypeFk, description) VALUES(?, ?, ?)
|
|
|
|
ON DUPLICATE KEY UPDATE description = CONCAT(vn.ticketObservation.description, VALUES(description),' ')`;
|
|
|
|
await Self.rawSql(query, [
|
|
|
|
observation.ticketFk,
|
|
|
|
observation.observationTypeFk,
|
|
|
|
observation.description
|
|
|
|
], transaction);
|
|
|
|
}
|
|
|
|
|
2018-11-28 11:03:48 +00:00
|
|
|
Self.importToNewRefundTicket = async (ctx, id) => {
|
2018-09-26 09:16:52 +00:00
|
|
|
let models = Self.app.models;
|
|
|
|
let token = ctx.req.accessToken;
|
|
|
|
let userId = token.userId;
|
|
|
|
let worker = await models.Worker.findOne({where: {userFk: userId}});
|
|
|
|
let obsevationType = await models.ObservationType.findOne({where: {description: 'comercial'}});
|
|
|
|
let agency = await models.AgencyMode.findOne({where: {code: 'refund'}});
|
|
|
|
let state = await models.State.findOne({where: {code: 'DELIVERED'}});
|
|
|
|
|
|
|
|
let filter = {
|
|
|
|
where: {id: id},
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
relation: 'ticket',
|
|
|
|
scope: {
|
|
|
|
fields: ['id', 'clientFk', 'warehouseFk', 'companyFk', 'addressFk']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
let claim = await models.Claim.findOne(filter);
|
|
|
|
|
|
|
|
let today = new Date();
|
|
|
|
|
|
|
|
let params = {
|
|
|
|
clientFk: claim.ticket().clientFk,
|
|
|
|
shipped: today,
|
|
|
|
landed: today,
|
|
|
|
warehouseFk: claim.ticket().warehouseFk,
|
|
|
|
companyFk: claim.ticket().companyFk,
|
|
|
|
addressFk: claim.ticket().addressFk,
|
|
|
|
agencyModeFk: agency.id,
|
|
|
|
userId: userId
|
|
|
|
};
|
|
|
|
|
|
|
|
let salesFilter = {
|
|
|
|
where: {claimFk: id},
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
relation: 'sale',
|
|
|
|
scope: {
|
|
|
|
fields: ['id', 'itemFk', 'concept', 'price', 'discount', 'reserved', 'isPicked', 'created', 'priceFixed', 'isPriceFixed']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
let transaction = await Self.beginTransaction({});
|
|
|
|
|
|
|
|
try {
|
2018-11-28 11:03:48 +00:00
|
|
|
let newRefundTicket = await models.Ticket.new(ctx, params, {transaction: transaction});
|
2018-10-10 07:59:42 +00:00
|
|
|
|
|
|
|
let observation = {
|
|
|
|
description: `Reclama ticket: ${claim.ticketFk}`,
|
|
|
|
ticketFk: newRefundTicket.id,
|
|
|
|
observationTypeFk: obsevationType.id
|
|
|
|
};
|
2018-09-26 09:16:52 +00:00
|
|
|
await saveObservation(observation, {transaction: transaction});
|
2018-10-10 07:59:42 +00:00
|
|
|
|
|
|
|
await models.TicketTracking.create({
|
|
|
|
ticketFk: newRefundTicket.id,
|
|
|
|
stateFk: state.id,
|
|
|
|
workerFk: worker.id
|
|
|
|
}, {transaction: transaction});
|
|
|
|
|
2018-09-26 09:16:52 +00:00
|
|
|
let salesToRefund = await models.ClaimBeginning.find(salesFilter);
|
2018-10-10 07:59:42 +00:00
|
|
|
let createdSales = await addSalesToTicket(salesToRefund, newRefundTicket.id, {transaction: transaction});
|
2018-09-26 09:16:52 +00:00
|
|
|
insertIntoClaimEnd(createdSales, id, worker.id, {transaction: transaction});
|
2018-10-10 07:59:42 +00:00
|
|
|
|
|
|
|
await Self.rawSql('CALL vn.ticketCalculateClon(?, ?)', [
|
|
|
|
newRefundTicket.id, claim.ticketFk
|
|
|
|
], {transaction: transaction});
|
|
|
|
|
2018-09-26 09:16:52 +00:00
|
|
|
await transaction.commit();
|
2018-10-10 07:59:42 +00:00
|
|
|
|
2018-09-28 11:55:21 +00:00
|
|
|
return newRefundTicket;
|
2018-09-26 09:16:52 +00:00
|
|
|
} catch (e) {
|
|
|
|
await transaction.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|