salix/db/routines/vn/triggers/address_afterUpdate.sql

41 lines
1.1 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`address_afterUpdate`
AFTER UPDATE ON `address`
FOR EACH ROW
BEGIN
-- Recargos de equivalencia distintos implican facturacion por consignatario
IF NEW.isEqualizated != OLD.isEqualizated THEN
IF
(SELECT COUNT(*) FROM
(
SELECT DISTINCT (isEqualizated = FALSE) as Equ
FROM address
WHERE clientFk = NEW.clientFk
) t1
) > 1
THEN
UPDATE client
SET hasToInvoiceByAddress = TRUE
WHERE id = NEW.clientFk;
END IF;
END IF;
IF NEW.isDefaultAddress AND NEW.isActive = FALSE THEN
CALL util.throw ('Cannot desactivate the default address');
END IF;
IF NOT (NEW.isEqualizated <=> OLD.isEqualizated) THEN
INSERT IGNORE INTO ticketRecalc (ticketFk)
SELECT id FROM ticket t
WHERE t.addressFk = NEW.id
AND t.refFk IS NULL;
END IF;
IF (NEW.clientFk <> OLD.clientFk OR NEW.isActive <> OLD.isActive OR NOT (NEW.provinceFk <=> OLD.provinceFk))
AND (SELECT client_hasDifferentCountries(NEW.clientFk)) THEN
UPDATE client
SET hasToInvoiceByAddress = TRUE
WHERE id = NEW.clientFk;
END IF;
END$$
DELIMITER ;