This commit is contained in:
parent
f300c5d047
commit
711b94c835
|
@ -1,5 +1,3 @@
|
|||
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
|
||||
|
||||
module.exports = Self => {
|
||||
Self.remoteMethodCtx('importToNewRefundTicket', {
|
||||
description: 'Imports lines from claimBeginning to a new ticket with specific shipped, landed dates, agency and company',
|
||||
|
@ -32,7 +30,13 @@ module.exports = Self => {
|
|||
{
|
||||
relation: 'ticket',
|
||||
scope: {
|
||||
fields: ['id', 'clientFk', 'warehouseFk', 'companyFk', 'addressFk']
|
||||
fields: ['id', 'clientFk', 'warehouseFk', 'companyFk', 'addressFk'],
|
||||
include: {
|
||||
relation: 'address',
|
||||
scope: {
|
||||
fields: ['nickname']
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
|
@ -64,23 +68,58 @@ module.exports = Self => {
|
|||
const worker = await models.Worker.findOne({
|
||||
where: {userFk: userId}
|
||||
}, options);
|
||||
|
||||
const obsevationType = await models.ObservationType.findOne({
|
||||
where: {description: 'comercial'}
|
||||
}, options);
|
||||
|
||||
const agencyMode = await models.AgencyMode.findOne({
|
||||
where: {code: 'refund'}
|
||||
}, options);
|
||||
|
||||
const state = await models.State.findOne({
|
||||
where: {code: 'DELIVERED'}
|
||||
}, options);
|
||||
|
||||
const zone = await models.Zone.findOne({
|
||||
where: {agencyModeFk: agencyMode.id}
|
||||
}, options);
|
||||
|
||||
const claim = await models.Claim.findOne(filter, options);
|
||||
let stmts = [];
|
||||
let stmt;
|
||||
const today = new Date();
|
||||
|
||||
stmt = new ParameterizedSQL('CALL vn.refund(?, @result)', [
|
||||
claim.ticketFk
|
||||
]);
|
||||
const newRefundTicket = await models.Ticket.create({
|
||||
clientFk: claim.ticket().clientFk,
|
||||
shipped: today,
|
||||
landed: today,
|
||||
nickname: claim.ticket().address().nickname,
|
||||
warehouseFk: claim.ticket().warehouseFk,
|
||||
companyFk: claim.ticket().companyFk,
|
||||
addressFk: claim.ticket().addressFk,
|
||||
agencyModeFk: agencyMode.id,
|
||||
zoneFk: zone.id
|
||||
}, options);
|
||||
|
||||
stmts.push(stmt);
|
||||
let ticketIndex = stmts.push('SELECT @result vNewRefundTicket') - 1;
|
||||
let sql = ParameterizedSQL.join(stmts, ';');
|
||||
let result = await Self.rawStmt(sql, options);
|
||||
let newRefundTicketId = result[ticketIndex][0].vNewRefundTicket;
|
||||
const newRefundTicket = await models.Ticket.findById(newRefundTicketId, null, options);
|
||||
const claimBeginnings = await models.ClaimBeginning.find(salesFilter, options);
|
||||
await saveObservation({
|
||||
description: `Reclama ticket: ${claim.ticketFk}`,
|
||||
ticketFk: newRefundTicket.id,
|
||||
observationTypeFk: obsevationType.id
|
||||
}, options);
|
||||
|
||||
await models.TicketTracking.create({
|
||||
ticketFk: newRefundTicket.id,
|
||||
stateFk: state.id,
|
||||
workerFk: worker.id
|
||||
}, options);
|
||||
|
||||
const salesToRefund = await models.ClaimBeginning.find(salesFilter, options);
|
||||
const createdSales = await addSalesToTicket(salesToRefund, newRefundTicket.id, options);
|
||||
await insertIntoClaimEnd(createdSales, id, worker.id, options);
|
||||
|
||||
await Self.rawSql('CALL vn.ticketCalculateClon(?, ?)', [
|
||||
newRefundTicket.id, claim.ticketFk
|
||||
], options);
|
||||
|
||||
await insertIntoClaimEnd(claimBeginnings, id, worker.id, options);
|
||||
await tx.commit();
|
||||
|
||||
return newRefundTicket;
|
||||
|
@ -89,11 +128,31 @@ module.exports = Self => {
|
|||
throw e;
|
||||
}
|
||||
};
|
||||
async function insertIntoClaimEnd(claimBeginnings, claimId, workerId, options) {
|
||||
const formatedSales = [];
|
||||
claimBeginnings.forEach(claimBeginning => {
|
||||
|
||||
async function addSalesToTicket(salesToRefund, ticketId, options) {
|
||||
let formatedSales = [];
|
||||
salesToRefund.forEach(sale => {
|
||||
let formatedSale = {
|
||||
saleFk: claimBeginning.saleFk,
|
||||
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
|
||||
};
|
||||
|
@ -101,4 +160,15 @@ module.exports = Self => {
|
|||
});
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -2,7 +2,6 @@ const app = require('vn-loopback/server/server');
|
|||
|
||||
describe('claimBeginning', () => {
|
||||
let ticket;
|
||||
let refundTicketObservations;
|
||||
let refundTicketSales;
|
||||
let salesInsertedInClaimEnd;
|
||||
|
||||
|
@ -16,7 +15,7 @@ describe('claimBeginning', () => {
|
|||
});
|
||||
|
||||
describe('importToNewRefundTicket()', () => {
|
||||
it('should create a new ticket with negative sales, save an observation, update the state and insert the negative sales into claimEnd', async() => {
|
||||
it('should create a new ticket with negative sales and insert the negative sales into claimEnd', async() => {
|
||||
let ctxOfSalesAssistant = {req: {accessToken: {userId: 21}}};
|
||||
let claimId = 1;
|
||||
ticket = await app.models.ClaimBeginning.importToNewRefundTicket(ctxOfSalesAssistant, claimId);
|
||||
|
@ -24,14 +23,10 @@ describe('claimBeginning', () => {
|
|||
await app.models.Ticket.findById(ticket.id);
|
||||
|
||||
refundTicketSales = await app.models.Sale.find({where: {ticketFk: ticket.id}});
|
||||
refundTicketObservations = await app.models.TicketObservation.find({where: {ticketFk: ticket.id}});
|
||||
let refundTicketState = await app.models.TicketState.findById(ticket.id);
|
||||
salesInsertedInClaimEnd = await app.models.ClaimEnd.find({where: {claimFk: claimId}});
|
||||
|
||||
expect(refundTicketSales.length).toEqual(1);
|
||||
expect(refundTicketSales[0].quantity).toEqual(-5);
|
||||
expect(refundTicketObservations[0].description).toEqual('Reclama ticket: 11');
|
||||
expect(refundTicketState.stateFk).toEqual(16);
|
||||
expect(salesInsertedInClaimEnd[0].saleFk).toEqual(refundTicketSales[0].id);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue