feat: añadido parametro que indica si genera uno o multiples ticket de abono
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Vicent Llopis 2023-02-14 10:58:41 +01:00
parent 350b572007
commit a5a0256e44
3 changed files with 85 additions and 33 deletions

View File

@ -35,8 +35,8 @@ module.exports = Self => {
const tickets = await models.Ticket.find(filter, myOptions);
const ticketsIds = tickets.map(ticket => ticket.id);
const refundedTickets = await models.Ticket.refund(ticketsIds, myOptions);
const createSingleTicket = true;
const refundedTickets = await models.Ticket.refund(ticketsIds, createSingleTicket, myOptions);
if (tx) await tx.commit();

View File

@ -12,6 +12,11 @@ module.exports = Self => {
arg: 'servicesIds',
type: ['number']
},
{
arg: 'createSingleTicket',
type: 'boolean',
required: false
}
],
returns: {
type: ['number'],
@ -23,7 +28,7 @@ module.exports = Self => {
}
});
Self.refund = async(salesIds, servicesIds, options) => {
Self.refund = async(salesIds, servicesIds, createSingleTicket = false, options) => {
const models = Self.app.models;
const myOptions = {};
let tx;
@ -63,38 +68,37 @@ module.exports = Self => {
const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))];
const refundTickets = [];
const now = Date.vnNew();
const mappedTickets = new Map();
const now = Date.vnNew();
for (let ticketId of ticketsIds) {
const filter = {include: {relation: 'address'}};
const ticket = await models.Ticket.findById(ticketId, filter, myOptions);
const refundTicket = await models.Ticket.create({
clientFk: ticket.clientFk,
shipped: now,
addressFk: ticket.address().id,
agencyModeFk: refundAgencyMode.id,
nickname: ticket.address().nickname,
warehouseFk: ticket.warehouseFk,
companyFk: ticket.companyFk,
landed: now,
zoneFk: refoundZoneId
}, myOptions);
refundTickets.push(refundTicket);
mappedTickets.set(ticketId, refundTicket.id);
await models.TicketRefund.create({
refundTicketFk: refundTicket.id,
originalTicketFk: ticket.id,
}, myOptions);
const [firstTicketId] = ticketsIds;
if (createSingleTicket) {
await createTicketRefund(
firstTicketId,
refundTickets,
mappedTickets,
now,
refundAgencyMode,
refoundZoneId,
myOptions
);
} else {
for (let ticketId of ticketsIds) {
await createTicketRefund(
ticketId,
refundTickets,
mappedTickets,
now,
refundAgencyMode,
refoundZoneId,
myOptions
);
}
}
for (const sale of sales) {
const refundTicketId = mappedTickets.get(sale.ticketFk);
const refundTicketId = await getTicketRefundId(createSingleTicket, sale.ticketFk, refundTickets);
const createdSale = await models.Sale.create({
ticketFk: refundTicketId,
itemFk: sale.itemFk,
@ -118,7 +122,7 @@ module.exports = Self => {
const services = await models.TicketService.find(servicesFilter, myOptions);
for (const service of services) {
const refundTicketId = mappedTickets.get(service.ticketFk);
const refundTicketId = await getTicketRefundId(createSingleTicket, service.ticketFk, refundTickets);
await models.TicketService.create({
description: service.description,
@ -139,4 +143,47 @@ module.exports = Self => {
throw e;
}
};
async function createTicketRefund(
ticketId,
refundTickets,
mappedTickets,
now,
refundAgencyMode,
refoundZoneId,
myOptions
) {
const models = Self.app.models;
const filter = {include: {relation: 'address'}};
const ticket = await models.Ticket.findById(ticketId, filter, myOptions);
const refundTicket = await models.Ticket.create({
clientFk: ticket.clientFk,
shipped: now,
addressFk: ticket.address().id,
agencyModeFk: refundAgencyMode.id,
nickname: ticket.address().nickname,
warehouseFk: ticket.warehouseFk,
companyFk: ticket.companyFk,
landed: now,
zoneFk: refoundZoneId
}, myOptions);
refundTickets.push(refundTicket);
mappedTickets.set(ticketId, refundTicket.id);
await models.TicketRefund.create({
refundTicketFk: refundTicket.id,
originalTicketFk: ticket.id,
}, myOptions);
}
async function getTicketRefundId(createSingleTicket, ticketId, refundTickets) {
if (createSingleTicket) {
const [firstRefundTicket] = refundTickets;
return firstRefundTicket.id;
} else return mappedTickets.get(ticketId);
}
};

View File

@ -8,6 +8,11 @@ module.exports = Self => {
type: ['number'],
required: true
},
{
arg: 'createSingleTicket',
type: 'boolean',
required: false
}
],
returns: {
type: ['number'],
@ -19,7 +24,7 @@ module.exports = Self => {
}
});
Self.refund = async(ticketsIds, options) => {
Self.refund = async(ticketsIds, createSingleTicket = false, options) => {
const models = Self.app.models;
const myOptions = {};
let tx;
@ -41,7 +46,7 @@ module.exports = Self => {
const services = await models.TicketService.find(filter, myOptions);
const servicesIds = services.map(service => service.id);
const refundedTickets = await models.Sale.refund(salesIds, servicesIds, myOptions);
const refundedTickets = await models.Sale.refund(salesIds, servicesIds, createSingleTicket, myOptions);
if (tx) await tx.commit();