2023-04-04 12:47:13 +00:00
|
|
|
DELIMITER $$
|
2024-08-20 08:06:10 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` TRIGGER `vn`.`client_beforeUpdate`
|
2023-04-04 12:47:13 +00:00
|
|
|
BEFORE UPDATE ON `client`
|
|
|
|
FOR EACH ROW
|
|
|
|
BEGIN
|
|
|
|
DECLARE vText VARCHAR(255) DEFAULT NULL;
|
2024-01-15 11:31:03 +00:00
|
|
|
DECLARE vPayMethodFk INT;
|
|
|
|
|
|
|
|
SET NEW.editorFk = account.myUser_getId();
|
|
|
|
|
|
|
|
IF NOT(NEW.credit <=> OLD.credit) THEN
|
|
|
|
INSERT INTO clientCredit
|
|
|
|
SET clientFk = NEW.id,
|
|
|
|
amount = NEW.credit,
|
|
|
|
workerFk = NEW.editorFk;
|
|
|
|
END IF;
|
2023-04-04 12:47:13 +00:00
|
|
|
-- Comprueba que el formato de los teléfonos es válido
|
|
|
|
|
|
|
|
IF !(NEW.phone <=> OLD.phone) AND (NEW.phone <> '') THEN
|
|
|
|
CALL pbx.phone_isValid(NEW.phone);
|
|
|
|
END IF;
|
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
IF !(NEW.mobile <=> OLD.mobile) AND (NEW.mobile <> '')THEN
|
2023-04-04 12:47:13 +00:00
|
|
|
CALL pbx.phone_isValid(NEW.mobile);
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
SELECT id INTO vPayMethodFk
|
|
|
|
FROM vn.payMethod
|
|
|
|
WHERE code = 'bankDraft';
|
|
|
|
|
|
|
|
IF NEW.payMethodFk = vPayMethodFk AND NEW.dueDay = 0 THEN
|
|
|
|
SET NEW.dueDay = 5;
|
2024-01-15 11:31:03 +00:00
|
|
|
END IF;
|
2023-04-04 12:47:13 +00:00
|
|
|
|
|
|
|
-- Avisar al comercial si ha llegado la documentación sepa/core
|
|
|
|
|
|
|
|
IF NEW.hasSepaVnl AND !OLD.hasSepaVnl THEN
|
|
|
|
SET vText = 'Sepa de VNL';
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
IF NEW.hasCoreVnl AND !OLD.hasCoreVnl THEN
|
|
|
|
SET vText = 'Core de VNL';
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
IF vText IS NOT NULL
|
2024-01-15 11:31:03 +00:00
|
|
|
THEN
|
2023-04-04 12:47:13 +00:00
|
|
|
INSERT INTO mail(receiver, replyTo, `subject`, body)
|
|
|
|
SELECT
|
|
|
|
CONCAT(IF(ac.id,u.name, 'jgallego'), '@verdnatura.es'),
|
2024-01-15 11:31:03 +00:00
|
|
|
'administracion@verdnatura.es',
|
|
|
|
CONCAT('Cliente ', NEW.id),
|
2023-04-04 12:47:13 +00:00
|
|
|
CONCAT('Recibida la documentación: ', vText)
|
|
|
|
FROM worker w
|
2023-08-30 10:48:57 +00:00
|
|
|
LEFT JOIN account.user u ON w.id = u.id AND u.active
|
2023-04-04 12:47:13 +00:00
|
|
|
LEFT JOIN account.account ac ON ac.id = u.id
|
|
|
|
WHERE w.id = NEW.salesPersonFk;
|
|
|
|
END IF;
|
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
IF NEW.salespersonFk IS NULL AND OLD.salespersonFk IS NOT NULL THEN
|
2023-04-04 12:47:13 +00:00
|
|
|
IF (SELECT COUNT(clientFk)
|
|
|
|
FROM clientProtected
|
2024-01-15 11:31:03 +00:00
|
|
|
WHERE clientFk = NEW.id
|
2023-04-04 12:47:13 +00:00
|
|
|
) > 0 THEN
|
|
|
|
CALL util.throw("HAS_CLIENT_PROTECTED");
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
2024-06-20 12:27:11 +00:00
|
|
|
IF NOT (NEW.salesPersonFk <=> OLD.salesPersonFk) THEN
|
2023-04-04 12:47:13 +00:00
|
|
|
SET NEW.lastSalesPersonFk = IFNULL(NEW.salesPersonFk, OLD.salesPersonFk);
|
|
|
|
END IF;
|
|
|
|
|
2024-06-20 12:27:11 +00:00
|
|
|
IF NOT (NEW.businessTypeFk <=> OLD.businessTypeFk) AND (NEW.businessTypeFk = 'individual' OR OLD.businessTypeFk = 'individual') THEN
|
2023-04-04 12:47:13 +00:00
|
|
|
SET NEW.isTaxDataChecked = 0;
|
|
|
|
END IF;
|
2024-11-14 08:27:38 +00:00
|
|
|
|
|
|
|
IF NOT (NEW.provinceFk <=> OLD.provinceFk)
|
|
|
|
OR (NEW.postcode <=> OLD.postcode) THEN
|
|
|
|
|
|
|
|
SET NEW.geoFk = client_getGeo(NEW.id);
|
|
|
|
END IF;
|
2023-04-04 12:47:13 +00:00
|
|
|
END$$
|
|
|
|
DELIMITER ;
|