diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 28be016fc..fb7569f70 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2911,8 +2911,7 @@ INSERT INTO `vn`.`workerConfig` (`id`, `businessUpdated`, `roleFk`, `payMethodFk INSERT INTO `vn`.`ticketRefund`(`refundTicketFk`, `originalTicketFk`) VALUES - (1, 12), - (8, 10); + (24, 7); INSERT INTO `vn`.`deviceProductionModels` (`code`) VALUES diff --git a/e2e/paths/05-ticket/01-sale/02_edit_sale.spec.js b/e2e/paths/05-ticket/01-sale/02_edit_sale.spec.js index b74f81253..9095eee0a 100644 --- a/e2e/paths/05-ticket/01-sale/02_edit_sale.spec.js +++ b/e2e/paths/05-ticket/01-sale/02_edit_sale.spec.js @@ -225,7 +225,7 @@ describe('Ticket Edit sale path', () => { }); it('should show error trying to delete a ticket with a refund', async() => { - await page.accessToSearchResult('16'); + await page.accessToSearchResult('6'); await page.waitToClick(selectors.ticketDescriptor.moreMenu); await page.waitToClick(selectors.ticketDescriptor.moreMenuDeleteTicket); await page.waitToClick(selectors.globalItems.acceptButton); diff --git a/modules/invoiceOut/back/methods/invoiceOut/transferInvoice.js b/modules/invoiceOut/back/methods/invoiceOut/transferInvoice.js index 5f2428539..782eaf6d8 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/transferInvoice.js +++ b/modules/invoiceOut/back/methods/invoiceOut/transferInvoice.js @@ -78,7 +78,7 @@ module.exports = Self => { const sales = await models.Sale.find(filterTicket, myOptions); const salesIds = sales.map(sale => sale.id); - const clonedTickets = await models.Sale.clone(ctx, salesIds, servicesIds, null, false, false, myOptions); + const clonedTickets = await models.Sale.clone(ctx, salesIds, servicesIds, null, false, myOptions); const clonedTicketIds = []; for (const clonedTicket of clonedTickets) { diff --git a/modules/ticket/back/methods/sale/clone.js b/modules/ticket/back/methods/sale/clone.js index a5ccb6de4..661b656df 100644 --- a/modules/ticket/back/methods/sale/clone.js +++ b/modules/ticket/back/methods/sale/clone.js @@ -1,5 +1,5 @@ module.exports = Self => { - Self.clone = async(ctx, salesIds, servicesIds, withWarehouse, group, negative, options) => { + Self.clone = async(ctx, salesIds, servicesIds, withWarehouse, negative, options) => { const models = Self.app.models; const myOptions = {}; let tx; @@ -28,8 +28,6 @@ module.exports = Self => { const mappedTickets = new Map(); - if (group) ticketsIds = [ticketsIds[0]]; - for (let ticketId of ticketsIds) { const newTicket = await createTicket( ctx, @@ -109,7 +107,10 @@ module.exports = Self => { const newTicket = await models.Ticket.new(ctx, myOptions); - if (negative) { + const ticketRefund = await models.TicketRefund.findOne({ + where: {refundTicketFk: ticketId} + }, myOptions); + if (negative && (withWarehouse || !ticketRefund?.id)) { await models.TicketRefund.create({ originalTicketFk: ticketId, refundTicketFk: newTicket.id diff --git a/modules/ticket/back/methods/sale/refund.js b/modules/ticket/back/methods/sale/refund.js index a7831e7e3..a19bb3df8 100644 --- a/modules/ticket/back/methods/sale/refund.js +++ b/modules/ticket/back/methods/sale/refund.js @@ -47,7 +47,6 @@ module.exports = Self => { salesIds, servicesIds, withWarehouse, - false, true, myOptions ); diff --git a/modules/ticket/back/methods/sale/specs/clone.spec.js b/modules/ticket/back/methods/sale/specs/clone.spec.js new file mode 100644 index 000000000..26506485a --- /dev/null +++ b/modules/ticket/back/methods/sale/specs/clone.spec.js @@ -0,0 +1,70 @@ +const models = require('vn-loopback/server/server').models; +const LoopBackContext = require('loopback-context'); + +describe('Ticket cloning - clone function', () => { + let ctx; + let options; + let tx; + + beforeEach(async() => { + ctx = { + req: { + accessToken: {userId: 9}, + headers: {origin: 'http://localhost'} + }, + args: {} + }; + + spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ + active: ctx.req + }); + + options = {transaction: tx}; + tx = await models.Sale.beginTransaction({}); + options.transaction = tx; + }); + + afterEach(async() => { + await tx.rollback(); + }); + + it('should create new tickets with cloned sales with warehouse', async() => { + const salesIds = [1, 2, 3]; + const servicesIds = []; + const withWarehouse = true; + const negative = false; + const newTickets = await models.Sale.clone(ctx, salesIds, servicesIds, withWarehouse, negative, options); + + expect(newTickets).toBeDefined(); + expect(newTickets.length).toBeGreaterThan(0); + }); + + it('should handle negative quantities correctly', async() => { + const negative = true; + const salesIds = [7, 8]; + const servicesIds = []; + const newTickets = await models.Sale.clone(ctx, salesIds, servicesIds, false, negative, options); + + for (const ticket of newTickets) { + const sales = await models.Sale.find({where: {ticketFk: ticket.id}}, options); + sales.forEach(sale => { + expect(sale.quantity).toBeLessThan(0); + }); + } + }); + + it('should create new components and services for cloned tickets', async() => { + const servicesIds = [2]; + const salesIds = [5]; + const newTickets = await models.Sale.clone(ctx, salesIds, servicesIds, false, false, options); + + for (const ticket of newTickets) { + const sale = await models.Sale.findOne({where: {ticketFk: ticket.id}}, options); + const components = await models.SaleComponent.find({where: {saleFk: sale.id}}, options); + const services = await models.TicketService.find({where: {ticketFk: ticket.id}}, options); + + expect(components.length).toBeGreaterThan(0); + expect(services.length).toBeGreaterThan(0); + } + }); +}); diff --git a/modules/ticket/back/methods/ticket/specs/setDeleted.spec.js b/modules/ticket/back/methods/ticket/specs/setDeleted.spec.js index b2e70697a..43bc2c2d9 100644 --- a/modules/ticket/back/methods/ticket/specs/setDeleted.spec.js +++ b/modules/ticket/back/methods/ticket/specs/setDeleted.spec.js @@ -3,7 +3,6 @@ const LoopBackContext = require('loopback-context'); describe('ticket setDeleted()', () => { const userId = 1106; - const employeeUser = 1110; const activeCtx = { accessToken: {userId: userId}, }; @@ -118,7 +117,7 @@ describe('ticket setDeleted()', () => { return value; }; - const ticketId = 12; + const ticketId = 7; await models.Ticket.setDeleted(ctx, ticketId, options); await tx.rollback(); diff --git a/modules/ticket/back/models/specs/sale.spec.js b/modules/ticket/back/models/specs/sale.spec.js index 4af44c991..d078dc8e2 100644 --- a/modules/ticket/back/models/specs/sale.spec.js +++ b/modules/ticket/back/models/specs/sale.spec.js @@ -132,7 +132,7 @@ describe('sale model ', () => { spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(9)); const tx = await models.Sale.beginTransaction({}); - const saleId = 13; + const saleId = 32; const newQuantity = -10; try {