diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 97d995c8d..1f8987721 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -304,7 +304,5 @@ "Mail not sent": "Se ha producido un fallo al enviar la factura al cliente [{{clientId}}]({{{clientUrl}}}), por favor revisa la dirección de correo electrónico", "The renew period has not been exceeded": "El periodo de renovación no ha sido superado", "Valid priorities": "Prioridades válidas: %d", - "There are no tickets to bill": "There are no tickets to bill", - "Negative basis of tickets": "Base negativa para los tickets: {{ticketsIds}}", - "Base negativa para los tickets: 10": "Base negativa para los tickets: 10" -} \ No newline at end of file + "Negative basis of tickets": "Base negativa para los tickets: {{ticketsIds}}" +} diff --git a/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js b/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js index f74fdc808..38e49120a 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js +++ b/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js @@ -74,7 +74,8 @@ module.exports = Self => { ], options); } - const invoiceId = await models.Ticket.makeInvoice(ctx, 'G', args.companyFk, options); + const invoiceType = 'G'; + const invoiceId = await models.Ticket.makeInvoice(ctx, invoiceType, args.companyFk, options); if (tx) await tx.commit(); diff --git a/modules/ticket/back/methods/ticket/canBeInvoiced.js b/modules/ticket/back/methods/ticket/canBeInvoiced.js index b297ad8c4..0f6cb476b 100644 --- a/modules/ticket/back/methods/ticket/canBeInvoiced.js +++ b/modules/ticket/back/methods/ticket/canBeInvoiced.js @@ -56,15 +56,21 @@ module.exports = function(Self) { const today = Date.vnNew(); - const invalidTickets = tickets.some(ticket => { + tickets.some(ticket => { const shipped = new Date(ticket.shipped); const shippingInFuture = shipped.getTime() > today.getTime(); - const isInvoiced = ticket.refFk; - const priceZero = ticket.totalWithVat == 0; + if (shippingInFuture) + throw new UserError(`Can't invoice to future`); - return isInvoiced || priceZero || shippingInFuture; + const isInvoiced = ticket.refFk; + if (isInvoiced) + throw new UserError(`This ticket is already invoiced`); + + const priceZero = ticket.totalWithVat == 0; + if (priceZero) + throw new UserError(`A ticket with an amount of zero can't be invoiced`); }); - return !(invalidTickets || hasAnyNegativeBase); + return true; }; }; diff --git a/modules/ticket/back/methods/ticket/makeInvoice.js b/modules/ticket/back/methods/ticket/makeInvoice.js index 955b84c2b..c4eef30c8 100644 --- a/modules/ticket/back/methods/ticket/makeInvoice.js +++ b/modules/ticket/back/methods/ticket/makeInvoice.js @@ -59,9 +59,7 @@ module.exports = function(Self) { fields: ['id', 'clientFk'] }, myOptions); - const ticketCanBeInvoiced = await models.Ticket.canBeInvoiced(ctx, ticketsIds, myOptions); - if (!ticketCanBeInvoiced || !ticketToInvoice.length) - throw new UserError(`Some of the selected tickets are not billable`); + await models.Ticket.canBeInvoiced(ctx, ticketsIds, myOptions); const [firstTicket] = tickets; const clientId = firstTicket.clientFk;