diff --git a/db/routines/vn/events/client_userDisable.sql b/db/routines/vn/events/client_userDisable.sql new file mode 100644 index 000000000..b3354f8fd --- /dev/null +++ b/db/routines/vn/events/client_userDisable.sql @@ -0,0 +1,8 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root`@`localhost` EVENT `vn`.`client_userDisable` + ON SCHEDULE EVERY 1 MONTH + STARTS '2023-06-01 00:00:00.000' + ON COMPLETION PRESERVE + ENABLE +DO CALL client_userDisable()$$ +DELIMITER ; diff --git a/db/routines/vn/events/clientsDisable.sql b/db/routines/vn/events/clientsDisable.sql deleted file mode 100644 index 35e6554a2..000000000 --- a/db/routines/vn/events/clientsDisable.sql +++ /dev/null @@ -1,25 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` EVENT `vn`.`clientsDisable` - ON SCHEDULE EVERY 1 MONTH - STARTS '2023-06-01 00:00:00.000' - ON COMPLETION PRESERVE - ENABLE -DO BEGIN - UPDATE account.user u - JOIN client c ON c.id = u.id - LEFT JOIN account.account a ON a.id = u.id - SET u.active = FALSE - WHERE c.typeFk = 'normal' - AND a.id IS NULL - AND u.active - AND c.created < util.VN_CURDATE() - INTERVAL 12 MONTH - AND u.id NOT IN ( - SELECT DISTINCT c.id - FROM client c - LEFT JOIN ticket t ON t.clientFk = c.id - WHERE c.salesPersonFk IS NOT NULL - OR t.created > util.VN_CURDATE() - INTERVAL 12 MONTH - OR shipped > util.VN_CURDATE() - INTERVAL 12 MONTH - ); -END$$ -DELIMITER ; diff --git a/db/routines/vn/procedures/client_userDisable.sql b/db/routines/vn/procedures/client_userDisable.sql new file mode 100644 index 000000000..f2ba65c1c --- /dev/null +++ b/db/routines/vn/procedures/client_userDisable.sql @@ -0,0 +1,33 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`client_userDisable`() +BEGIN +/** +* Desactiva los clientes inactivos en los Ășltimos X meses. +*/ + DECLARE vMonths INT; + + SELECT monthsToDisableUser INTO vMonths + FROM clientConfig; + + IF vMonths IS NULL THEN + CALL util.throw('Config parameter not set'); + END IF; + + UPDATE account.user u + JOIN client c ON c.id = u.id + LEFT JOIN account.account a ON a.id = u.id + SET u.active = FALSE + WHERE c.typeFk = 'normal' + AND a.id IS NULL + AND u.active + AND c.created < util.VN_CURDATE() - INTERVAL vMonths MONTH + AND u.id NOT IN ( + SELECT DISTINCT c.id + FROM client c + LEFT JOIN ticket t ON t.clientFk = c.id + WHERE c.salesPersonFk IS NOT NULL + OR t.created > util.VN_CURDATE() - INTERVAL vMonths MONTH + OR shipped > util.VN_CURDATE() - INTERVAL vMonths MONTH + ); +END$$ +DELIMITER ; diff --git a/db/versions/11055-wheatPaniculata/00-firstScript.sql b/db/versions/11055-wheatPaniculata/00-firstScript.sql new file mode 100644 index 000000000..6eec62fd8 --- /dev/null +++ b/db/versions/11055-wheatPaniculata/00-firstScript.sql @@ -0,0 +1,5 @@ +ALTER TABLE vn.clientConfig + ADD monthsToDisableUser int(10) unsigned DEFAULT NULL NULL; + +UPDATE IGNORE vn.clientConfig + SET monthsToDisableUser = 12; \ No newline at end of file