2018-09-26 09:16:52 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('importToNewRefundTicket', {
|
2022-07-01 07:15:07 +00:00
|
|
|
description: 'Import lines of claimBeginning to new ticket with shipped, landed dates, agency and company',
|
2018-09-26 09:16:52 +00:00
|
|
|
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'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-19 18:20:19 +00:00
|
|
|
Self.importToNewRefundTicket = async(ctx, id, options) => {
|
2019-06-21 06:26:59 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
const token = ctx.req.accessToken;
|
|
|
|
const userId = token.userId;
|
|
|
|
const filter = {
|
2018-09-26 09:16:52 +00:00
|
|
|
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']
|
|
|
|
}
|
|
|
|
}
|
2018-09-26 09:16:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
2019-06-21 06:26:59 +00:00
|
|
|
const salesFilter = {
|
2018-09-26 09:16:52 +00:00
|
|
|
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']
|
2018-09-26 09:16:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
2021-03-19 18:20:19 +00:00
|
|
|
let tx;
|
2023-06-01 06:32:06 +00:00
|
|
|
const myOptions = {userId: ctx.req.accessToken.userId};
|
2021-03-19 18:20:19 +00:00
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:16:52 +00:00
|
|
|
try {
|
2019-06-21 06:26:59 +00:00
|
|
|
const worker = await models.Worker.findOne({
|
2023-09-20 09:58:36 +00:00
|
|
|
where: {id: userId}
|
2021-03-19 18:20:19 +00:00
|
|
|
}, myOptions);
|
2019-09-04 11:39:01 +00:00
|
|
|
|
|
|
|
const obsevationType = await models.ObservationType.findOne({
|
2021-05-17 10:27:26 +00:00
|
|
|
where: {code: 'salesPerson'}
|
2021-03-19 18:20:19 +00:00
|
|
|
}, myOptions);
|
2019-09-04 11:39:01 +00:00
|
|
|
|
|
|
|
const agencyMode = await models.AgencyMode.findOne({
|
|
|
|
where: {code: 'refund'}
|
2021-03-19 18:20:19 +00:00
|
|
|
}, myOptions);
|
2019-09-04 11:39:01 +00:00
|
|
|
|
|
|
|
const state = await models.State.findOne({
|
|
|
|
where: {code: 'DELIVERED'}
|
2021-03-19 18:20:19 +00:00
|
|
|
}, myOptions);
|
2019-09-04 11:39:01 +00:00
|
|
|
|
|
|
|
const zone = await models.Zone.findOne({
|
|
|
|
where: {agencyModeFk: agencyMode.id}
|
2021-03-19 18:20:19 +00:00
|
|
|
}, myOptions);
|
2019-09-04 11:39:01 +00:00
|
|
|
|
2021-03-19 18:20:19 +00:00
|
|
|
const claim = await models.Claim.findOne(filter, myOptions);
|
2023-01-16 14:18:24 +00:00
|
|
|
const today = Date.vnNew();
|
2019-09-04 11:39:01 +00:00
|
|
|
|
|
|
|
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
|
2021-03-19 18:20:19 +00:00
|
|
|
}, myOptions);
|
2019-09-04 11:39:01 +00:00
|
|
|
|
2023-04-21 12:00:28 +00:00
|
|
|
await models.TicketRefund.create({
|
|
|
|
refundTicketFk: newRefundTicket.id,
|
|
|
|
originalTicketFk: claim.ticket().id
|
|
|
|
}, myOptions);
|
|
|
|
|
2019-09-04 11:39:01 +00:00
|
|
|
await saveObservation({
|
|
|
|
description: `Reclama ticket: ${claim.ticketFk}`,
|
|
|
|
ticketFk: newRefundTicket.id,
|
|
|
|
observationTypeFk: obsevationType.id
|
2021-03-19 18:20:19 +00:00
|
|
|
}, myOptions);
|
2019-09-04 11:39:01 +00:00
|
|
|
|
|
|
|
await models.TicketTracking.create({
|
|
|
|
ticketFk: newRefundTicket.id,
|
|
|
|
stateFk: state.id,
|
|
|
|
workerFk: worker.id
|
2021-03-19 18:20:19 +00:00
|
|
|
}, myOptions);
|
2019-09-04 11:39:01 +00:00
|
|
|
|
2021-03-19 18:20:19 +00:00
|
|
|
const salesToRefund = await models.ClaimBeginning.find(salesFilter, myOptions);
|
|
|
|
const createdSales = await addSalesToTicket(salesToRefund, newRefundTicket.id, myOptions);
|
|
|
|
await insertIntoClaimEnd(createdSales, id, worker.id, myOptions);
|
2019-09-04 11:39:01 +00:00
|
|
|
|
|
|
|
await Self.rawSql('CALL vn.ticketCalculateClon(?, ?)', [
|
|
|
|
newRefundTicket.id, claim.ticketFk
|
2021-03-19 18:20:19 +00:00
|
|
|
], myOptions);
|
2019-09-04 11:39:01 +00:00
|
|
|
|
2021-03-19 18:20:19 +00:00
|
|
|
if (tx) await tx.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) {
|
2021-03-19 18:20:19 +00:00
|
|
|
if (tx) await tx.rollback();
|
2018-09-26 09:16:52 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
2019-09-04 11:39:01 +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) {
|
2019-06-21 06:26:59 +00:00
|
|
|
const formatedSales = [];
|
2019-09-04 11:39:01 +00:00
|
|
|
createdSales.forEach(sale => {
|
2019-06-21 06:26:59 +00:00
|
|
|
let formatedSale = {
|
2019-09-04 11:39:01 +00:00
|
|
|
saleFk: sale.id,
|
2019-06-21 06:26:59 +00:00
|
|
|
claimFk: claimId,
|
|
|
|
workerFk: workerId
|
|
|
|
};
|
|
|
|
formatedSales.push(formatedSale);
|
|
|
|
});
|
|
|
|
await Self.app.models.ClaimEnd.create(formatedSales, options);
|
|
|
|
}
|
2019-09-04 11:39:01 +00:00
|
|
|
|
|
|
|
async function saveObservation(observation, options) {
|
|
|
|
const query = `INSERT INTO vn.ticketObservation (ticketFk, observationTypeFk, description) VALUES(?, ?, ?)
|
2023-01-16 14:18:24 +00:00
|
|
|
ON DUPLICATE KEY
|
2019-09-04 11:39:01 +00:00
|
|
|
UPDATE description = CONCAT(vn.ticketObservation.description, VALUES(description),' ')`;
|
|
|
|
await Self.rawSql(query, [
|
|
|
|
observation.ticketFk,
|
|
|
|
observation.observationTypeFk,
|
|
|
|
observation.description
|
|
|
|
], options);
|
|
|
|
}
|
2018-09-26 09:16:52 +00:00
|
|
|
};
|