diff --git a/loopback/locale/en.json b/loopback/locale/en.json index 06538a524..e447c5d9d 100644 --- a/loopback/locale/en.json +++ b/loopback/locale/en.json @@ -150,7 +150,7 @@ "Receipt's bank was not found": "Receipt's bank was not found", "This receipt was not compensated": "This receipt was not compensated", "Client's email was not found": "Client's email was not found", - "Tickets with associated refunds": "Tickets with associated refunds can't be deleted. This ticket is associated with refund Nº %d", + "Tickets with associated refunds": "Tickets with associated refunds can't be deleted. This ticket is associated with refund Nº %s", "It is not possible to modify tracked sales": "It is not possible to modify tracked sales", "It is not possible to modify sales that their articles are from Floramondo": "It is not possible to modify sales that their articles are from Floramondo", "It is not possible to modify cloned sales": "It is not possible to modify cloned sales", diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 377691ae6..8768e0ba3 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -272,7 +272,7 @@ "Invoice date can't be less than max date": "La fecha de factura no puede ser inferior a la fecha límite", "Warehouse inventory not set": "El almacén inventario no está establecido", "This locker has already been assigned": "Esta taquilla ya ha sido asignada", - "Tickets with associated refunds": "No se pueden borrar tickets con abonos asociados. Este ticket está asociado al abono Nº %d", + "Tickets with associated refunds": "No se pueden borrar tickets con abonos asociados. Este ticket está asociado al abono Nº %s", "Not exist this branch": "La rama no existe", "This ticket cannot be signed because it has not been boxed": "Este ticket no puede firmarse porque no ha sido encajado", "Collection does not exist": "La colección no existe", diff --git a/loopback/locale/fr.json b/loopback/locale/fr.json index 49584ef0e..107e669f3 100644 --- a/loopback/locale/fr.json +++ b/loopback/locale/fr.json @@ -272,7 +272,7 @@ "Invoice date can't be less than max date": "La date de la facture ne peut pas être inférieure à la date limite", "Warehouse inventory not set": "L'inventaire de l'entrepôt n'est pas établi", "This locker has already been assigned": "Ce casier a déjà été assigné", - "Tickets with associated refunds": "Vous ne pouvez pas supprimer des tickets avec des remboursements associés. Ce ticket est associé au remboursement Nº %d", + "Tickets with associated refunds": "Vous ne pouvez pas supprimer des tickets avec des remboursements associés. Ce ticket est associé au remboursement Nº %s", "Not exist this branch": "La branche n'existe pas", "This ticket cannot be signed because it has not been boxed": "Ce ticket ne peut pas être signé car il n'a pas été emballé", "Collection does not exist": "La collection n'existe pas", diff --git a/modules/ticket/back/methods/ticket/setDeleted.js b/modules/ticket/back/methods/ticket/setDeleted.js index 2afdf44ac..125827c5e 100644 --- a/modules/ticket/back/methods/ticket/setDeleted.js +++ b/modules/ticket/back/methods/ticket/setDeleted.js @@ -43,11 +43,22 @@ module.exports = Self => { // Check if ticket has refunds const ticketRefunds = await models.TicketRefund.find({ - where: {originalTicketFk: id}, - fields: ['id']} - , myOptions); - if (ticketRefunds.length > 0) - throw new UserError('Tickets with associated refunds', 'TICKET_REFUND', ticketRefunds[0].id); + include: [ + {relation: 'refundTicket'} + ], + where: {originalTicketFk: id} + }, myOptions); + + const allDeleted = ticketRefunds.every(refund => refund.refundTicket().isDeleted); + + if (ticketRefunds?.length && !allDeleted) { + const notDeleted = []; + for (const refund of ticketRefunds) + if (!refund.refundTicket().isDeleted) notDeleted.push(refund.refundTicket().id); + + throw new UserError('Tickets with associated refunds', 'TICKET_REFUND', + notDeleted.join(', ')); + } // Check if has sales with shelving const canDeleteTicketWithPartPrepared = diff --git a/modules/ticket/back/methods/ticket/specs/setDeleted.spec.js b/modules/ticket/back/methods/ticket/specs/setDeleted.spec.js index ed9347714..782c31c02 100644 --- a/modules/ticket/back/methods/ticket/specs/setDeleted.spec.js +++ b/modules/ticket/back/methods/ticket/specs/setDeleted.spec.js @@ -76,21 +76,42 @@ describe('ticket setDeleted()', () => { } }); - it('should show error trying to delete a ticket with a refund', async() => { - const tx = await models.Ticket.beginTransaction({}); - let error; - try { - const options = {transaction: tx}; + describe('ticket with refund setDeleted()', () => { + it('should show error trying to delete a ticket with a refund', async() => { + const tx = await models.Ticket.beginTransaction({}); + let error; + try { + const options = {transaction: tx}; - const ticketId = 8; - await models.Ticket.setDeleted(ctx, ticketId, options); + const ticketId = 8; + await models.Ticket.setDeleted(ctx, ticketId, options); - await tx.rollback(); - } catch (e) { - await tx.rollback(); - error = e; - } + await tx.rollback(); + } catch (e) { + await tx.rollback(); + error = e; + } - expect(error.message).toContain('Tickets with associated refunds'); + expect(error.message).toContain('Tickets with associated refunds'); + }); + + it('should delete a ticket with a refund if ticket refund is deleted', async() => { + const tx = await models.Ticket.beginTransaction({}); + try { + const options = {transaction: tx}; + + const ticketId = 8; + const ticketRefundId = 24; + await models.Ticket.updateAll({id: ticketRefundId}, {isDeleted: true}, options); + await models.Ticket.setDeleted(ctx, ticketId, options); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + error = e; + } + + expect(error.message).not.toContain('Tickets with associated refunds'); + }); }); });