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

189 lines
6.3 KiB
JavaScript
Raw Normal View History

module.exports = Self => {
Self.remoteMethodCtx('importToNewRefundTicket', {
description: 'Import lines of claimBeginning to new ticket with 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'
}
});
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 = {
where: {id: id},
include: [
{
relation: 'ticket',
scope: {
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']
}
}
]
};
let tx;
2023-06-01 06:32:06 +00:00
const myOptions = {userId: ctx.req.accessToken.userId};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
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}
}, myOptions);
const obsevationType = await models.ObservationType.findOne({
where: {code: 'salesPerson'}
}, myOptions);
const agencyMode = await models.AgencyMode.findOne({
where: {code: 'refund'}
}, myOptions);
const state = await models.State.findOne({
where: {code: 'DELIVERED'}
}, myOptions);
const zone = await models.Zone.findOne({
where: {agencyModeFk: agencyMode.id}
}, myOptions);
const claim = await models.Claim.findOne(filter, myOptions);
2023-01-16 14:18:24 +00:00
const today = Date.vnNew();
const newRefundTicket = await models.Ticket.create({
clientFk: claim.ticket().clientFk,
shipped: today,
landed: today,
2024-05-07 13:33:18 +00:00
nickname: `Abono del: ${claim.ticketFk}`,
warehouseFk: claim.ticket().warehouseFk,
companyFk: claim.ticket().companyFk,
addressFk: claim.ticket().addressFk,
agencyModeFk: agencyMode.id,
zoneFk: zone.id
}, myOptions);
await models.TicketRefund.create({
refundTicketFk: newRefundTicket.id,
originalTicketFk: claim.ticket().id
}, myOptions);
await saveObservation({
description: `Reclama ticket: ${claim.ticketFk}`,
ticketFk: newRefundTicket.id,
observationTypeFk: obsevationType.id
}, myOptions);
await models.Ticket.state(ctx, {
ticketFk: newRefundTicket.id,
stateFk: state.id,
userFk: worker.id
}, myOptions);
const salesToRefund = await models.ClaimBeginning.find(salesFilter, myOptions);
const createdSales = await addSalesToTicket(salesToRefund, newRefundTicket.id, myOptions);
await insertIntoClaimEnd(createdSales, id, worker.id, myOptions);
await Self.rawSql('CALL vn.ticketCalculateClon(?, ?)', [
newRefundTicket.id, claim.ticketFk
], myOptions);
if (tx) await tx.commit();
2018-10-10 07:59:42 +00:00
return newRefundTicket;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
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 = [];
createdSales.forEach(sale => {
2019-06-21 06:26:59 +00:00
let formatedSale = {
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);
}
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
UPDATE description = CONCAT(vn.ticketObservation.description, VALUES(description),' ')`;
await Self.rawSql(query, [
observation.ticketFk,
observation.observationTypeFk,
observation.description
], options);
}
};