salix/db/routines/vn/procedures/shelving_clean.sql

39 lines
995 B
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`shelving_clean`()
BEGIN
DELETE FROM shelving
WHERE length(code) > 3
AND parked < TIMESTAMPADD(WEEK,-1,util.VN_CURDATE());
DELETE FROM itemShelving
WHERE visible <= 0
AND created < TIMESTAMPADD(MONTH,-1,util.VN_CURDATE());
DELETE ish.*
FROM itemShelving ish
JOIN vn.shelving sh ON sh.id = ish.shelvingFk
WHERE sh.parkingFk IS NULL
AND ish.created < TIMESTAMPADD(WEEK,-1,util.VN_CURDATE());
UPDATE shelving sh
LEFT JOIN itemShelving its ON its.shelvingFk = sh.id
SET isPrinted = 0,
parkingFk = NULL
WHERE its.id IS NULL
AND sh.isRecyclable
AND (
sh.parked IS NULL
OR
sh.parked < util.VN_CURDATE() - INTERVAL 2 WEEK
)
AND IF(code REGEXP '^[A-Za-z]{2}[0-9]', LEFT (code, 2) NOT IN (
SELECT DISTINCT LEFT(sh.code, 2)
FROM itemShelving its
JOIN shelving sh ON sh.id = its.shelvingFk
WHERE sh.code REGEXP '^[A-Za-z]{2}[0-9]'
), TRUE);
END$$
DELIMITER ;