salix/db/routines/vn/functions/sale_hasComponentLack.sql

28 lines
695 B
MySQL
Raw Normal View History

DELIMITER $$
2024-08-20 08:06:10 +00:00
CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`sale_hasComponentLack`(
vSelf INT
)RETURNS tinyint(1)
READS SQL DATA
BEGIN
/**
* Check if a sales line has all the required components.
*
* @param vSelf Id de sale
* @return BOOL
*/
DECLARE vHasComponentLack TINYINT(1);
WITH componentRequired AS(
SELECT COUNT(*) total
FROM vn.component
WHERE isRequired
)SELECT SUM(IF(c.isRequired, TRUE, FALSE)) <> cr.total INTO vHasComponentLack
FROM vn.sale s
JOIN componentRequired cr
LEFT JOIN vn.saleComponent sc ON sc.saleFk = s.id
LEFT JOIN vn.component c ON c.id = sc.componentFk
WHERE s.id = vSelf;
RETURN vHasComponentLack;
END$$
DELIMITER ;