salix/modules/claim/back/methods/claim-beginning/importToNewRefundTicket.js

175 lines
5.9 KiB
JavaScript
Raw Normal View History

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'
}
});
2019-01-16 11:55:38 +00:00
Self.importToNewRefundTicket = async(ctx, id) => {
2019-06-21 06:26:59 +00:00
const models = Self.app.models;
const token = ctx.req.accessToken;
const userId = token.userId;
const tx = await Self.beginTransaction({});
const filter = {
where: {id: id},
include: [
{
relation: 'ticket',
scope: {
2019-09-04 11:39:01 +00:00
fields: ['id', 'clientFk', 'warehouseFk', 'companyFk', 'addressFk'],
include: {
relation: 'address',
scope: {
fields: ['nickname']
}
}
}
}
]
};
2019-06-21 06:26:59 +00:00
const salesFilter = {
where: {claimFk: id},
include: [
{
relation: 'sale',
scope: {
2019-06-21 06:26:59 +00:00
fields: [
'id',
'itemFk',
'concept',
'price',
'discount',
'reserved',
'isPicked',
'created',
'priceFixed',
'isPriceFixed']
}
}
]
};
try {
let options = {transaction: tx};
2019-06-21 06:26:59 +00:00
const worker = await models.Worker.findOne({
where: {userFk: userId}
}, options);
2019-06-21 06:26:59 +00:00
const obsevationType = await models.ObservationType.findOne({
where: {description: 'comercial'}
}, options);
2018-10-10 07:59:42 +00:00
2019-09-04 11:39:01 +00:00
const agencyMode = await models.AgencyMode.findOne({
2019-06-21 06:26:59 +00:00
where: {code: 'refund'}
}, options);
const state = await models.State.findOne({
where: {code: 'DELIVERED'}
}, options);
2019-09-04 11:39:01 +00:00
const zone = await models.Zone.findOne({
where: {agencyModeFk: agencyMode.id}
}, options);
2019-06-21 06:26:59 +00:00
const claim = await models.Claim.findOne(filter, options);
const today = new Date();
2019-09-04 11:39:01 +00:00
const newRefundTicket = await models.Ticket.create({
2019-06-21 06:26:59 +00:00
clientFk: claim.ticket().clientFk,
shipped: today,
landed: today,
2019-09-04 11:39:01 +00:00
nickname: claim.ticket().address().nickname,
2019-06-21 06:26:59 +00:00
warehouseFk: claim.ticket().warehouseFk,
companyFk: claim.ticket().companyFk,
addressFk: claim.ticket().addressFk,
2019-09-04 11:39:01 +00:00
agencyModeFk: agencyMode.id,
zoneFk: zone.id
2019-06-21 06:26:59 +00:00
}, options);
await saveObservation({
2018-10-10 07:59:42 +00:00
description: `Reclama ticket: ${claim.ticketFk}`,
ticketFk: newRefundTicket.id,
observationTypeFk: obsevationType.id
2019-06-21 06:26:59 +00:00
}, options);
2018-10-10 07:59:42 +00:00
await models.TicketTracking.create({
ticketFk: newRefundTicket.id,
stateFk: state.id,
workerFk: worker.id
}, options);
2018-10-10 07:59:42 +00:00
2019-06-21 06:26:59 +00:00
const salesToRefund = await models.ClaimBeginning.find(salesFilter, options);
const createdSales = await addSalesToTicket(salesToRefund, newRefundTicket.id, options);
await insertIntoClaimEnd(createdSales, id, worker.id, options);
2018-10-10 07:59:42 +00:00
await Self.rawSql('CALL vn.ticketCalculateClon(?, ?)', [
newRefundTicket.id, claim.ticketFk
], options);
2018-10-10 07:59:42 +00:00
await tx.commit();
2018-10-10 07:59:42 +00:00
return newRefundTicket;
} catch (e) {
await tx.rollback();
throw e;
}
};
2019-06-21 06:26:59 +00:00
async function addSalesToTicket(salesToRefund, ticketId, options) {
let formatedSales = [];
salesToRefund.forEach(sale => {
let formatedSale = {
itemFk: sale.sale().itemFk,
ticketFk: ticketId,
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
};
formatedSales.push(formatedSale);
});
return await Self.app.models.Sale.create(formatedSales, options);
}
async function insertIntoClaimEnd(createdSales, claimId, workerId, options) {
const formatedSales = [];
createdSales.forEach(sale => {
let formatedSale = {
saleFk: sale.id,
claimFk: claimId,
workerFk: workerId
};
formatedSales.push(formatedSale);
});
await Self.app.models.ClaimEnd.create(formatedSales, options);
}
async function saveObservation(observation, options) {
const 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
], options);
}
};