From a6556e594d11b28301a36f4d1fc328d869da5b25 Mon Sep 17 00:00:00 2001 From: Bernat Exposito Date: Tue, 6 Mar 2018 11:22:57 +0100 Subject: [PATCH] update trigger client, #154 --- .../changes/1.0.1/05-updateTriggerClient.sql | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 services/db/changes/1.0.1/05-updateTriggerClient.sql diff --git a/services/db/changes/1.0.1/05-updateTriggerClient.sql b/services/db/changes/1.0.1/05-updateTriggerClient.sql new file mode 100644 index 000000000..bacbb9aa1 --- /dev/null +++ b/services/db/changes/1.0.1/05-updateTriggerClient.sql @@ -0,0 +1,58 @@ +DROP TRIGGER IF EXISTS `vn2008`.`ClientesBeforeUpdate`; + +DELIMITER $$ +USE `vn2008`$$ +CREATE DEFINER=`root`@`%` TRIGGER `ClientesBeforeUpdate` + BEFORE UPDATE ON `Clientes` + FOR EACH ROW +BEGIN + DECLARE vText VARCHAR(255) DEFAULT NULL; + DECLARE isAlreadyUsedIf BOOLEAN; + + SELECT (COUNT(*) > 1) INTO isAlreadyUsedIf + FROM Clientes + WHERE `IF` = TRIM(NEW.`IF`); + + IF isAlreadyUsedIf THEN + CALL util.throw ('Error. El NIF/CIF está repetido'); + END IF; + + -- Comprueba que el formato de los teléfonos es válido + + IF !(NEW.telefono <=> OLD.telefono) THEN + CALL pbx.phoneIsValid (NEW.telefono); + END IF; + + IF !(NEW.movil <=> OLD.movil) THEN + CALL pbx.phoneIsValid (NEW.movil); + END IF; + + IF !(NEW.fax <=> OLD.fax) THEN + CALL pbx.phoneIsValid (NEW.fax); + END IF; + + -- Avisar al comercial si ha llegado la documentación sepa/core + + IF NEW.sepavnl AND !OLD.sepavnl THEN + SET vText = 'Sepa de VNL'; + END IF; + + IF NEW.corevnl AND !OLD.corevnl THEN + SET vText = 'Core de VNL'; + END IF; + + IF vText IS NOT NULL + THEN + INSERT INTO mail(`to`, reply_to, subject, text) + SELECT + CONCAT(IF(ac.id,name, 'jgallego'), '@verdnatura.es'), + 'administracion@verdnatura.es', + CONCAT('Cliente ', NEW.Id_Cliente), + CONCAT('Recibida la documentación: ', vText) + FROM Trabajadores t + LEFT JOIN account.user u ON t.user_id = u.id AND active + LEFT JOIN account.account ac ON ac.id = u.id + WHERE t.Id_Trabajador = NEW.Id_Trabajador; + END IF; +END$$ +DELIMITER ;