From 1e3a7e5f9113aac0beb95b052c2b8be745934995 Mon Sep 17 00:00:00 2001 From: alexm Date: Tue, 2 Jan 2024 13:17:10 +0100 Subject: [PATCH] refs #5914 refactor: invoiceTickets --- ...00-fixInvoiceCorrectionConstraintsName.sql | 7 +++++ .../{235001 => 240001}/00-getTaxBases.sql | 0 .../01-newHasAnyPositiveBase.sql | 2 +- .../01-refactorHasAnyNegativeBase.sql | 2 +- .../back/models/invoice-correction.json | 27 +++++++++++++++++++ .../back/methods/ticket/canBeInvoiced.js | 7 ----- .../back/methods/ticket/invoiceTickets.js | 19 +++++++------ .../ticket/back/methods/ticket/makeInvoice.js | 3 +-- .../ticket/specs/canBeInvoiced.spec.js | 20 +++----------- 9 files changed, 50 insertions(+), 37 deletions(-) create mode 100644 db/changes/240001/00-fixInvoiceCorrectionConstraintsName.sql rename db/changes/{235001 => 240001}/00-getTaxBases.sql (100%) rename db/changes/{235001 => 240001}/01-newHasAnyPositiveBase.sql (91%) rename db/changes/{235001 => 240001}/01-refactorHasAnyNegativeBase.sql (91%) diff --git a/db/changes/240001/00-fixInvoiceCorrectionConstraintsName.sql b/db/changes/240001/00-fixInvoiceCorrectionConstraintsName.sql new file mode 100644 index 000000000..426afea90 --- /dev/null +++ b/db/changes/240001/00-fixInvoiceCorrectionConstraintsName.sql @@ -0,0 +1,7 @@ +ALTER TABLE `vn`.`invoiceCorrection` DROP FOREIGN KEY `cplusInvoiceTyoeFk`; +ALTER TABLE `vn`.`invoiceCorrection` DROP FOREIGN KEY `invoiceCorrectionType_Fk33`; +ALTER TABLE `vn`.`invoiceCorrection` DROP FOREIGN KEY `invoiceCorrection_ibfk_1`; + +ALTER TABLE `vn`.`invoiceCorrection` ADD CONSTRAINT `siiTypeInvoiceOut_FK` FOREIGN KEY (`siiTypeInvoiceOutFk`) REFERENCES `vn`.`siiTypeInvoiceOut`(id) ON UPDATE CASCADE; +ALTER TABLE `vn`.`invoiceCorrection` ADD CONSTRAINT `invoiceCorrectionType_FK` FOREIGN KEY (`invoiceCorrectionTypeFk`) REFERENCES `vn`.`invoiceCorrectionType`(id) ON UPDATE CASCADE; +ALTER TABLE `vn`.`invoiceCorrection` ADD CONSTRAINT `cplusRectificationType_FK` FOREIGN KEY (`cplusRectificationTypeFk`) REFERENCES `vn`.`cplusRectificationType`(id) ON UPDATE CASCADE; diff --git a/db/changes/235001/00-getTaxBases.sql b/db/changes/240001/00-getTaxBases.sql similarity index 100% rename from db/changes/235001/00-getTaxBases.sql rename to db/changes/240001/00-getTaxBases.sql diff --git a/db/changes/235001/01-newHasAnyPositiveBase.sql b/db/changes/240001/01-newHasAnyPositiveBase.sql similarity index 91% rename from db/changes/235001/01-newHasAnyPositiveBase.sql rename to db/changes/240001/01-newHasAnyPositiveBase.sql index c9e7e8e05..70fd51bcc 100644 --- a/db/changes/235001/01-newHasAnyPositiveBase.sql +++ b/db/changes/240001/01-newHasAnyPositiveBase.sql @@ -14,7 +14,7 @@ BEGIN CALL getTaxBases(); - SELECT positive > 0 INTO hasAnyPositiveBase + SELECT positive INTO hasAnyPositiveBase FROM tmp.taxBases; DROP TEMPORARY TABLE diff --git a/db/changes/235001/01-refactorHasAnyNegativeBase.sql b/db/changes/240001/01-refactorHasAnyNegativeBase.sql similarity index 91% rename from db/changes/235001/01-refactorHasAnyNegativeBase.sql rename to db/changes/240001/01-refactorHasAnyNegativeBase.sql index 65822fc95..f851ee6b9 100644 --- a/db/changes/235001/01-refactorHasAnyNegativeBase.sql +++ b/db/changes/240001/01-refactorHasAnyNegativeBase.sql @@ -14,7 +14,7 @@ BEGIN CALL getTaxBases(); - SELECT negative > 0 INTO hasAnyNegativeBase + SELECT negative INTO hasAnyNegativeBase FROM tmp.taxBases; DROP TEMPORARY TABLE diff --git a/modules/invoiceOut/back/models/invoice-correction.json b/modules/invoiceOut/back/models/invoice-correction.json index 5924c9232..58f6f63b7 100644 --- a/modules/invoiceOut/back/models/invoice-correction.json +++ b/modules/invoiceOut/back/models/invoice-correction.json @@ -26,6 +26,33 @@ "invoiceCorrectionTypeFk": { "type": "number", "required": true + }, + "relations": { + "correcting": { + "type": "belongsTo", + "model": "InvoiceOut", + "foreignKey": "correctingFk" + }, + "corrected": { + "type": "belongsTo", + "model": "InvoiceOut", + "foreignKey": "correctedFk" + }, + "cplusRectificationType": { + "type": "belongsTo", + "model": "cplusRectificationType", + "foreignKey": "cplusRectificationTypeFk" + }, + "siiTypeInvoiceOut": { + "type": "belongsTo", + "model": "siiTypeInvoiceOut", + "foreignKey": "siiTypeInvoiceOutFk" + }, + "invoiceCorrectionType": { + "type": "belongsTo", + "model": "invoiceCorrectionType", + "foreignKey": "invoiceCorrectionTypeFk" + } } } } diff --git a/modules/ticket/back/methods/ticket/canBeInvoiced.js b/modules/ticket/back/methods/ticket/canBeInvoiced.js index 75d13a508..855a864c2 100644 --- a/modules/ticket/back/methods/ticket/canBeInvoiced.js +++ b/modules/ticket/back/methods/ticket/canBeInvoiced.js @@ -17,11 +17,6 @@ module.exports = function(Self) { type: 'boolean' } ], - returns: { - arg: 'data', - type: 'boolean', - root: true - }, http: { path: `/canBeInvoiced`, verb: 'get' @@ -63,7 +58,5 @@ module.exports = function(Self) { if (ticketsIds.length == 1 && priceZero) throw new UserError(`A ticket with an amount of zero can't be invoiced`); }); - - return true; }; }; diff --git a/modules/ticket/back/methods/ticket/invoiceTickets.js b/modules/ticket/back/methods/ticket/invoiceTickets.js index f1793773b..57f623289 100644 --- a/modules/ticket/back/methods/ticket/invoiceTickets.js +++ b/modules/ticket/back/methods/ticket/invoiceTickets.js @@ -61,20 +61,19 @@ module.exports = function(Self) { if (!isSameClient) throw new UserError(`You can't invoice tickets from multiple clients`); - const client = await models.Client.findById(clientId, { - fields: ['id', 'hasToInvoiceByAddress'] + const {hasToInvoiceByAddress} = await models.Client.findById(clientId, { + fields: ['hasToInvoiceByAddress'] }, myOptions); - let ticketsByAddress = {[firstTicket.addressFk]: ticketsIds}; - if (client.hasToInvoiceByAddress) { - ticketsByAddress = tickets.reduce((group, ticket) => { - const {addressFk} = ticket; + let ticketsByAddress = hasToInvoiceByAddress + ? Object.values(tickets.reduce((group, {id, addressFk}) => { group[addressFk] = group[addressFk] ?? []; - group[addressFk].push(ticket.id); + group[addressFk].push(id); return group; - }, {}); - } - for (const ticketIds of Object.values(ticketsByAddress)) + }, {})) + : [[ticketsIds]]; + + for (const ticketIds of ticketsByAddress) invoicesIds.push(await createInvoice(ctx, companyId, ticketIds, invoiceCorrection, myOptions)); if (tx) await tx.commit(); diff --git a/modules/ticket/back/methods/ticket/makeInvoice.js b/modules/ticket/back/methods/ticket/makeInvoice.js index 32aa03f9d..83222a4ee 100644 --- a/modules/ticket/back/methods/ticket/makeInvoice.js +++ b/modules/ticket/back/methods/ticket/makeInvoice.js @@ -106,8 +106,7 @@ module.exports = function(Self) { ); } - if (resultInvoice.id) - await Self.rawSql('CALL invoiceOutBooking(?)', [resultInvoice.id], myOptions); + await Self.rawSql('CALL invoiceOutBooking(?)', [resultInvoice.id], myOptions); if (tx) await tx.commit(); diff --git a/modules/ticket/back/methods/ticket/specs/canBeInvoiced.spec.js b/modules/ticket/back/methods/ticket/specs/canBeInvoiced.spec.js index e443ed6d3..1509b87df 100644 --- a/modules/ticket/back/methods/ticket/specs/canBeInvoiced.spec.js +++ b/modules/ticket/back/methods/ticket/specs/canBeInvoiced.spec.js @@ -27,10 +27,7 @@ describe('ticket canBeInvoiced()', () => { WHERE id IN (?) `, [ticketId], options); - const canBeInvoiced = await models.Ticket.canBeInvoiced(ctx, [ticketId], false, options); - - expect(canBeInvoiced).toEqual(false); - + await models.Ticket.canBeInvoiced(ctx, [ticketId], false, options); await tx.rollback(); } catch (e) { error = e; @@ -59,10 +56,7 @@ describe('ticket canBeInvoiced()', () => { WHERE id IN (?) `, [ticketId], options); - const canBeInvoiced = await models.Ticket.canBeInvoiced(ctx, [ticketId], false, options); - - expect(canBeInvoiced).toEqual(false); - + await models.Ticket.canBeInvoiced(ctx, [ticketId], false, options); await tx.rollback(); } catch (e) { error = e; @@ -95,10 +89,7 @@ describe('ticket canBeInvoiced()', () => { WHERE id IN (?) `, [ticketId], options); - const canBeInvoiced = await models.Ticket.canBeInvoiced(ctx, [ticketId], false, options); - - expect(canBeInvoiced).toEqual(false); - + await models.Ticket.canBeInvoiced(ctx, [ticketId], false, options); await tx.rollback(); } catch (e) { error = e; @@ -123,10 +114,7 @@ describe('ticket canBeInvoiced()', () => { WHERE id IN (?) `, [ticketId], options); - const canBeInvoiced = await models.Ticket.canBeInvoiced(ctx, [ticketId], false, options); - - expect(canBeInvoiced).toEqual(true); - + await models.Ticket.canBeInvoiced(ctx, [ticketId], false, options); await tx.rollback(); } catch (e) { await tx.rollback();