From 556b403a5b3ee3a8f8a92846ec43ec2ab2ed134d Mon Sep 17 00:00:00 2001 From: carlossa Date: Wed, 13 Dec 2023 11:29:50 +0100 Subject: [PATCH] refs #5525 letter debtor st --- db/changes/235001/00-supplier.sql | 11 ++++---- .../letter-debtor-st/letter-debtor-st.js | 28 +++++++++++++++---- .../email/letter-debtor-st/sql/client.sql | 19 ++++++------- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/db/changes/235001/00-supplier.sql b/db/changes/235001/00-supplier.sql index eebd8b4f2..704479130 100644 --- a/db/changes/235001/00-supplier.sql +++ b/db/changes/235001/00-supplier.sql @@ -1,10 +1,9 @@ ALTER TABLE `vn`.`company` MODIFY COLUMN `supplierAccountFk` mediumint(8) unsigned DEFAULT NULL NULL COMMENT 'Cuenta por defecto para ingresos desde este pais'; -CREATE TABLE `vn`.`supplierAccountI18n` ( - supplierAccountFk mediumint(8) unsigned NOT NULL, - countryFk mediumint(8) unsigned NOT NULL, - FOREIGN KEY (supplierAccountFk) REFERENCES supplierAccount(id), - FOREIGN KEY (countryFk) REFERENCES country(id) -); +ALTER TABLE `vn`.`supplierAccount` +ADD COLUMN `countryFk` mediumint(8) unsigned DEFAULT NULL, +ADD CONSTRAINT `fk_supplierAccount_country` + FOREIGN KEY (`countryFk`) REFERENCES `country` (`id`) ON UPDATE CASCADE; + diff --git a/print/templates/email/letter-debtor-st/letter-debtor-st.js b/print/templates/email/letter-debtor-st/letter-debtor-st.js index 5d0a11baf..76df43d70 100755 --- a/print/templates/email/letter-debtor-st/letter-debtor-st.js +++ b/print/templates/email/letter-debtor-st/letter-debtor-st.js @@ -7,12 +7,28 @@ module.exports = { name: 'letter-debtor-st', async serverPrefetch() { this.debtor = await db.findOne(` - SELECT sa.iban, be.name bankName - FROM supplierAccount sa - JOIN bankEntity be ON sa.bankEntityFk = be.id - 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 { diff --git a/print/templates/email/letter-debtor-st/sql/client.sql b/print/templates/email/letter-debtor-st/sql/client.sql index d5da5d0d5..013a3b7d6 100644 --- a/print/templates/email/letter-debtor-st/sql/client.sql +++ b/print/templates/email/letter-debtor-st/sql/client.sql @@ -1,10 +1,9 @@ -SELECT - c.dueDay, - c.iban, - sa.iban, - be.name AS bankName -FROM client c - JOIN company AS cny - JOIN supplierAccount AS sa ON sa.id = cny.supplierAccountFk - JOIN bankEntity be ON be.id = sa.bankEntityFk -WHERE c.id = ? AND cny.id = ? \ No newline at end of file +SELECT c.dueDay, + sa.iban, + be.name bankName + FROM client c + JOIN supplierAccount sa ON sa.id = cny.supplierAccountFk + JOIN bankEntity be ON be.id = sa.bankEntityFk + JOIN company cny + WHERE c.id = ? + AND cny.id = ?