Merge pull request '5180-invoice-out_refund' (!1325) from 5180-invoice-out_refund into dev
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
Reviewed-on: #1325 Reviewed-by: Javi Gallego <jgallego@verdnatura.es> Reviewed-by: Alex Moreno <alexm@verdnatura.es>
This commit is contained in:
commit
f7cf6e26fc
|
@ -0,0 +1,3 @@
|
||||||
|
INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`)
|
||||||
|
VALUES
|
||||||
|
('ItemConfig', '*', '*', 'ALLOW', 'ROLE', 'employee');
|
|
@ -35,8 +35,7 @@ module.exports = Self => {
|
||||||
const tickets = await models.Ticket.find(filter, myOptions);
|
const tickets = await models.Ticket.find(filter, myOptions);
|
||||||
|
|
||||||
const ticketsIds = tickets.map(ticket => ticket.id);
|
const ticketsIds = tickets.map(ticket => ticket.id);
|
||||||
|
const refundedTickets = await models.Ticket.refund(ticketsIds, true, myOptions);
|
||||||
const refundedTickets = await models.Ticket.refund(ticketsIds, myOptions);
|
|
||||||
|
|
||||||
if (tx) await tx.commit();
|
if (tx) await tx.commit();
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ describe('InvoiceOut refund()', () => {
|
||||||
try {
|
try {
|
||||||
const result = await models.InvoiceOut.refund('T1111111', options);
|
const result = await models.InvoiceOut.refund('T1111111', options);
|
||||||
|
|
||||||
expect(result.length).toEqual(2);
|
expect(result.length).toEqual(1);
|
||||||
|
|
||||||
await tx.rollback();
|
await tx.rollback();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
@ -12,6 +12,11 @@ module.exports = Self => {
|
||||||
arg: 'servicesIds',
|
arg: 'servicesIds',
|
||||||
type: ['number']
|
type: ['number']
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
arg: 'createSingleTicket',
|
||||||
|
type: 'boolean',
|
||||||
|
required: false
|
||||||
|
}
|
||||||
],
|
],
|
||||||
returns: {
|
returns: {
|
||||||
type: ['number'],
|
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 models = Self.app.models;
|
||||||
const myOptions = {};
|
const myOptions = {};
|
||||||
let tx;
|
let tx;
|
||||||
|
@ -63,11 +68,93 @@ module.exports = Self => {
|
||||||
const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))];
|
const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))];
|
||||||
|
|
||||||
const refundTickets = [];
|
const refundTickets = [];
|
||||||
|
|
||||||
const now = Date.vnNew();
|
|
||||||
const mappedTickets = new Map();
|
const mappedTickets = new Map();
|
||||||
|
const now = Date.vnNew();
|
||||||
|
|
||||||
|
const [firstTicketId] = ticketsIds;
|
||||||
|
if (createSingleTicket) {
|
||||||
|
await createTicketRefund(
|
||||||
|
firstTicketId,
|
||||||
|
refundTickets,
|
||||||
|
mappedTickets,
|
||||||
|
now,
|
||||||
|
refundAgencyMode,
|
||||||
|
refoundZoneId,
|
||||||
|
myOptions
|
||||||
|
);
|
||||||
|
} else {
|
||||||
for (let ticketId of ticketsIds) {
|
for (let ticketId of ticketsIds) {
|
||||||
|
await createTicketRefund(
|
||||||
|
ticketId,
|
||||||
|
refundTickets,
|
||||||
|
mappedTickets,
|
||||||
|
now,
|
||||||
|
refundAgencyMode,
|
||||||
|
refoundZoneId,
|
||||||
|
myOptions
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const sale of sales) {
|
||||||
|
const refundTicketId = await getTicketRefundId(createSingleTicket, sale.ticketFk, refundTickets, mappedTickets);
|
||||||
|
|
||||||
|
const createdSale = await models.Sale.create({
|
||||||
|
ticketFk: refundTicketId,
|
||||||
|
itemFk: sale.itemFk,
|
||||||
|
quantity: - sale.quantity,
|
||||||
|
concept: sale.concept,
|
||||||
|
price: sale.price,
|
||||||
|
discount: sale.discount,
|
||||||
|
}, myOptions);
|
||||||
|
|
||||||
|
const components = sale.components();
|
||||||
|
for (const component of components)
|
||||||
|
component.saleFk = createdSale.id;
|
||||||
|
|
||||||
|
await models.SaleComponent.create(components, myOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (servicesIds && servicesIds.length > 0) {
|
||||||
|
const servicesFilter = {
|
||||||
|
where: {id: {inq: servicesIds}}
|
||||||
|
};
|
||||||
|
const services = await models.TicketService.find(servicesFilter, myOptions);
|
||||||
|
|
||||||
|
for (const service of services) {
|
||||||
|
const refundTicketId = await getTicketRefundId(createSingleTicket, service.ticketFk, refundTickets, mappedTickets);
|
||||||
|
|
||||||
|
await models.TicketService.create({
|
||||||
|
description: service.description,
|
||||||
|
quantity: - service.quantity,
|
||||||
|
price: service.price,
|
||||||
|
taxClassFk: service.taxClassFk,
|
||||||
|
ticketFk: refundTicketId,
|
||||||
|
ticketServiceTypeFk: service.ticketServiceTypeFk,
|
||||||
|
}, myOptions);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tx) await tx.commit();
|
||||||
|
|
||||||
|
return refundTickets;
|
||||||
|
} catch (e) {
|
||||||
|
if (tx) await tx.rollback();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
async function createTicketRefund(
|
||||||
|
ticketId,
|
||||||
|
refundTickets,
|
||||||
|
mappedTickets,
|
||||||
|
now,
|
||||||
|
refundAgencyMode,
|
||||||
|
refoundZoneId,
|
||||||
|
myOptions
|
||||||
|
) {
|
||||||
|
const models = Self.app.models;
|
||||||
|
|
||||||
const filter = {include: {relation: 'address'}};
|
const filter = {include: {relation: 'address'}};
|
||||||
const ticket = await models.Ticket.findById(ticketId, filter, myOptions);
|
const ticket = await models.Ticket.findById(ticketId, filter, myOptions);
|
||||||
|
|
||||||
|
@ -93,50 +180,10 @@ module.exports = Self => {
|
||||||
}, myOptions);
|
}, myOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const sale of sales) {
|
async function getTicketRefundId(createSingleTicket, ticketId, refundTickets, mappedTickets) {
|
||||||
const refundTicketId = mappedTickets.get(sale.ticketFk);
|
if (createSingleTicket) {
|
||||||
const createdSale = await models.Sale.create({
|
const [firstRefundTicket] = refundTickets;
|
||||||
ticketFk: refundTicketId,
|
return firstRefundTicket.id;
|
||||||
itemFk: sale.itemFk,
|
} else return mappedTickets.get(ticketId);
|
||||||
quantity: - sale.quantity,
|
|
||||||
concept: sale.concept,
|
|
||||||
price: sale.price,
|
|
||||||
discount: sale.discount,
|
|
||||||
}, myOptions);
|
|
||||||
|
|
||||||
const components = sale.components();
|
|
||||||
for (const component of components)
|
|
||||||
component.saleFk = createdSale.id;
|
|
||||||
|
|
||||||
await models.SaleComponent.create(components, myOptions);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (servicesIds && servicesIds.length > 0) {
|
|
||||||
const servicesFilter = {
|
|
||||||
where: {id: {inq: servicesIds}}
|
|
||||||
};
|
|
||||||
const services = await models.TicketService.find(servicesFilter, myOptions);
|
|
||||||
|
|
||||||
for (const service of services) {
|
|
||||||
const refundTicketId = mappedTickets.get(service.ticketFk);
|
|
||||||
|
|
||||||
await models.TicketService.create({
|
|
||||||
description: service.description,
|
|
||||||
quantity: - service.quantity,
|
|
||||||
price: service.price,
|
|
||||||
taxClassFk: service.taxClassFk,
|
|
||||||
ticketFk: refundTicketId,
|
|
||||||
ticketServiceTypeFk: service.ticketServiceTypeFk,
|
|
||||||
}, myOptions);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tx) await tx.commit();
|
|
||||||
|
|
||||||
return refundTickets;
|
|
||||||
} catch (e) {
|
|
||||||
if (tx) await tx.rollback();
|
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
const models = require('vn-loopback/server/server').models;
|
const models = require('vn-loopback/server/server').models;
|
||||||
const LoopBackContext = require('loopback-context');
|
const LoopBackContext = require('loopback-context');
|
||||||
|
|
||||||
describe('sale refund()', () => {
|
describe('Sale refund()', () => {
|
||||||
const userId = 5;
|
const userId = 5;
|
||||||
const activeCtx = {
|
const activeCtx = {
|
||||||
accessToken: {userId: userId},
|
accessToken: {userId: userId},
|
||||||
|
@ -22,7 +22,7 @@ describe('sale refund()', () => {
|
||||||
try {
|
try {
|
||||||
const options = {transaction: tx};
|
const options = {transaction: tx};
|
||||||
|
|
||||||
const response = await models.Sale.refund(salesIds, servicesIds, options);
|
const response = await models.Sale.refund(salesIds, servicesIds, false, options);
|
||||||
|
|
||||||
expect(response.length).toBeGreaterThanOrEqual(1);
|
expect(response.length).toBeGreaterThanOrEqual(1);
|
||||||
|
|
||||||
|
@ -40,7 +40,8 @@ describe('sale refund()', () => {
|
||||||
try {
|
try {
|
||||||
const options = {transaction: tx};
|
const options = {transaction: tx};
|
||||||
|
|
||||||
const tickets = await models.Sale.refund(salesIds, servicesIds, options);
|
const createSingleTicket = false;
|
||||||
|
const tickets = await models.Sale.refund(salesIds, servicesIds, createSingleTicket, options);
|
||||||
|
|
||||||
const ticketsIds = tickets.map(ticket => ticket.id);
|
const ticketsIds = tickets.map(ticket => ticket.id);
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,11 @@ module.exports = Self => {
|
||||||
type: ['number'],
|
type: ['number'],
|
||||||
required: true
|
required: true
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
arg: 'createSingleTicket',
|
||||||
|
type: 'boolean',
|
||||||
|
required: false
|
||||||
|
}
|
||||||
],
|
],
|
||||||
returns: {
|
returns: {
|
||||||
type: ['number'],
|
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 models = Self.app.models;
|
||||||
const myOptions = {};
|
const myOptions = {};
|
||||||
let tx;
|
let tx;
|
||||||
|
@ -41,7 +46,7 @@ module.exports = Self => {
|
||||||
const services = await models.TicketService.find(filter, myOptions);
|
const services = await models.TicketService.find(filter, myOptions);
|
||||||
const servicesIds = services.map(service => service.id);
|
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();
|
if (tx) await tx.commit();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue