From ea31d3bebb71923242cbf0ddf0818827ce65c9ba Mon Sep 17 00:00:00 2001 From: carlossa Date: Wed, 13 Dec 2023 11:46:48 +0100 Subject: [PATCH] refs #5525 debtor-nd --- .../letter-debtor-nd/letter-debtor-nd.js | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/print/templates/email/letter-debtor-nd/letter-debtor-nd.js b/print/templates/email/letter-debtor-nd/letter-debtor-nd.js index 2f4a25cfe..781cd21f4 100755 --- a/print/templates/email/letter-debtor-nd/letter-debtor-nd.js +++ b/print/templates/email/letter-debtor-nd/letter-debtor-nd.js @@ -7,12 +7,28 @@ module.exports = { name: 'letter-debtor-nd', async serverPrefetch() { this.debtor = await db.findOne(` - SELECT sa.iban, be.name bankName - FROM supplierAccount sa - LEFT JOIN bankEntity be ON sa.bankEntityFk = be.id - LEFT JOIN client c ON c.countryFk = sa.countryFk - WHERE c.id = ? OR (c.id IS NULL AND c.countryFk = sa.countryFk)`, - [this.id]); + SELECT sa.iban, + be.name bankName, + sa.countryFk, + c.countryFk + FROM supplierAccount sa + JOIN bankEntity be ON sa.bankEntityFk = be.id + LEFT JOIN company co ON co.supplierAccountFk = sa.id + LEFT JOIN client c ON c.countryFk = sa.countryFk + WHERE c.id = ? + ORDER BY co.id DESC + LIMIT 1; + `, [this.id]); + if (!this.debtor.length) { + this.debtor = await db.findOne(` + SELECT sa.iban, + be.name bankName + FROM supplierAccount sa + JOIN bankEntity be ON sa.bankEntityFk = be.id + JOIN company co ON co.supplierAccountFk = sa.id + WHERE co.code = ?; + `, [this.companyCode]); + } }, data() { return {