#655 [FALTA PLANIFICACION] claim.action añadir acciones
This commit is contained in:
parent
9bc8687a85
commit
f84e11c5b5
|
@ -0,0 +1,129 @@
|
||||||
|
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'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
async function addSalesToTicket(salesToRefund, newRefundTicket, transaction) {
|
||||||
|
let promises = [];
|
||||||
|
salesToRefund.forEach(sale => {
|
||||||
|
let formatedSale = {
|
||||||
|
itemFk: sale.sale().itemFk,
|
||||||
|
ticketFk: newRefundTicket,
|
||||||
|
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
|
||||||
|
};
|
||||||
|
promises.push(Self.app.models.Sale.create(formatedSale, transaction));
|
||||||
|
});
|
||||||
|
return await Promise.all(promises);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function insertIntoClaimEnd(createdSales, claimId, workerId, transaction) {
|
||||||
|
let promises = [];
|
||||||
|
createdSales.forEach(sale => {
|
||||||
|
let formatedSale = {
|
||||||
|
saleFk: sale.id,
|
||||||
|
claimFk: claimId,
|
||||||
|
workerFk: workerId
|
||||||
|
};
|
||||||
|
promises.push(Self.app.models.ClaimEnd.create(formatedSale, transaction));
|
||||||
|
});
|
||||||
|
return await Promise.all(promises);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
Self.importToNewRefundTicket = async(ctx, id) => {
|
||||||
|
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 {
|
||||||
|
let newRefundTicket = await models.Ticket.new(params, {transaction: transaction});
|
||||||
|
let observation = {description: `Reclama ticket: ${claim.ticketFk}`, ticketFk: newRefundTicket, observationTypeFk: obsevationType.id};
|
||||||
|
await saveObservation(observation, {transaction: transaction});
|
||||||
|
await models.TicketTracking.create({ticketFk: newRefundTicket, stateFk: state.id, workerFk: worker.id}, {transaction: transaction});
|
||||||
|
let salesToRefund = await models.ClaimBeginning.find(salesFilter);
|
||||||
|
let createdSales = await addSalesToTicket(salesToRefund, newRefundTicket, {transaction: transaction});
|
||||||
|
insertIntoClaimEnd(createdSales, id, worker.id, {transaction: transaction});
|
||||||
|
await Self.rawSql('CALL vn.ticketCalculateClon(?)', [newRefundTicket], {transaction: transaction});
|
||||||
|
await transaction.commit();
|
||||||
|
} catch (e) {
|
||||||
|
await transaction.rollback();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
Loading…
Reference in New Issue