2019-09-04 10:17:33 +00:00
|
|
|
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
|
|
|
|
|
2018-09-26 09:16:52 +00:00
|
|
|
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 = {
|
2018-09-26 09:16:52 +00:00
|
|
|
where: {id: id},
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
relation: 'ticket',
|
|
|
|
scope: {
|
|
|
|
fields: ['id', 'clientFk', 'warehouseFk', 'companyFk', 'addressFk']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2019-06-12 15:31:45 +00:00
|
|
|
let options = {transaction: tx};
|
2019-06-21 06:26:59 +00:00
|
|
|
const worker = await models.Worker.findOne({
|
|
|
|
where: {userFk: userId}
|
|
|
|
}, options);
|
|
|
|
const claim = await models.Claim.findOne(filter, options);
|
2019-09-04 10:17:33 +00:00
|
|
|
let stmts = [];
|
|
|
|
let stmt;
|
|
|
|
|
|
|
|
stmt = new ParameterizedSQL('CALL vn.refund(?, @result)', [
|
|
|
|
claim.ticketFk
|
|
|
|
]);
|
|
|
|
|
|
|
|
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 insertIntoClaimEnd(claimBeginnings, id, worker.id, options);
|
2019-06-12 15:31:45 +00:00
|
|
|
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) {
|
2019-06-12 15:31:45 +00:00
|
|
|
await tx.rollback();
|
2018-09-26 09:16:52 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
2019-09-04 10:17:33 +00:00
|
|
|
async function insertIntoClaimEnd(claimBeginnings, claimId, workerId, options) {
|
2019-06-21 06:26:59 +00:00
|
|
|
const formatedSales = [];
|
2019-09-04 10:17:33 +00:00
|
|
|
claimBeginnings.forEach(claimBeginning => {
|
2019-06-21 06:26:59 +00:00
|
|
|
let formatedSale = {
|
2019-09-04 10:17:33 +00:00
|
|
|
saleFk: claimBeginning.saleFk,
|
2019-06-21 06:26:59 +00:00
|
|
|
claimFk: claimId,
|
|
|
|
workerFk: workerId
|
|
|
|
};
|
|
|
|
formatedSales.push(formatedSale);
|
|
|
|
});
|
|
|
|
await Self.app.models.ClaimEnd.create(formatedSales, options);
|
|
|
|
}
|
2018-09-26 09:16:52 +00:00
|
|
|
};
|