From 52094146135bf9b6f832805aa3820271a504843d Mon Sep 17 00:00:00 2001 From: joan Date: Tue, 19 Oct 2021 13:46:17 +0200 Subject: [PATCH] feat(closure): should mail the PDF invoice invoiceable clients Refs: 3211 --- db/dump/fixtures.sql | 3 ++- print/methods/closure.js | 39 +++++++++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 66bd821cf..d37fc9c75 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -485,7 +485,8 @@ INSERT INTO `vn`.`invoiceOutSerial` (`code`, `description`, `isTaxed`, `taxAreaF VALUES ('A', 'Global nacional', 1, 'NATIONAL', 0), ('T', 'Española rapida', 1, 'NATIONAL', 0), - ('V', 'Intracomunitaria global', 0, 'CEE', 1); + ('V', 'Intracomunitaria global', 0, 'CEE', 1), + ('M', 'Múltiple nacional', 1, 'NATIONAL', 0); INSERT INTO `vn`.`invoiceOut`(`id`, `serial`, `amount`, `issued`,`clientFk`, `created`, `companyFk`, `dued`, `booked`, `bankFk`, `hasPdf`) VALUES diff --git a/print/methods/closure.js b/print/methods/closure.js index daa2d5e7c..450bd0ed2 100644 --- a/print/methods/closure.js +++ b/print/methods/closure.js @@ -190,8 +190,7 @@ module.exports = app => { try { await db.rawSql(`CALL vn.ticket_closeByTicket(?)`, [ticket.id]); - const hasToInvoice = ticket.hasToInvoice && ticket.hasDailyInvoice; - if (!ticket.salesPersonFk || !ticket.isToBeMailed || hasToInvoice) continue; + if (!ticket.salesPersonFk || !ticket.isToBeMailed) continue; if (!ticket.recipient) { const body = `No se ha podido enviar el albarán ${ticket.id} @@ -206,15 +205,35 @@ module.exports = app => { continue; } - const args = Object.assign({ - ticketId: ticket.id, - recipientId: ticket.clientFk, - recipient: ticket.recipient, - replyTo: ticket.salesPersonEmail - }, reqArgs); + const hasToInvoice = ticket.hasToInvoice && ticket.hasDailyInvoice; + if (hasToInvoice) { + const invoiceId = await db.findValue(` + SELECT io.id + FROM ticket t + JOIN invoiceOut io ON io.ref = t.refFk + WHERE t.id = ? + `, [ticket.id]); - const email = new Email('delivery-note-link', args); - await email.send(); + const args = Object.assign({ + invoiceId: invoiceId, + recipientId: ticket.clientFk, + recipient: ticket.recipient, + replyTo: ticket.salesPersonEmail + }, reqArgs); + + const email = new Email('invoice', args); + await email.send(); + } else { + const args = Object.assign({ + ticketId: ticket.id, + recipientId: ticket.clientFk, + recipient: ticket.recipient, + replyTo: ticket.salesPersonEmail + }, reqArgs); + + const email = new Email('delivery-note-link', args); + await email.send(); + } } catch (error) { // Domain not found if (error.responseCode == 450)