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

25 lines
665 B
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`sale_setProblem`(
vProblemCode VARCHAR(25)
)
BEGIN
/**
* Update column sale.problem with a problem code
* @param vProblemCode Code to set or unset
* @table tmp.sale(saleFk, hasProblem, isProblemCalcNeeded)
*/
UPDATE tmp.sale ts
JOIN sale s ON s.id = ts.saleFk
SET s.problem = NULL
WHERE NOT ts.isProblemCalcNeeded;
DELETE FROM tmp.sale WHERE NOT isProblemCalcNeeded;
UPDATE sale s
JOIN tmp.sale ts ON ts.saleFk = s.id
SET s.problem = CONCAT(
IF(ts.hasProblem,
CONCAT(s.problem, ',', vProblemCode),
REPLACE(s.problem, vProblemCode , '')));
END$$
DELIMITER ;