59 lines
1.5 KiB
SQL
59 lines
1.5 KiB
SQL
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 ;
|