Created
December 26, 2017 11:53
-
-
Save FelipeGangrel/68298f43636fec5032ae4cae006eb12d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP FUNCTION IF EXISTS `getMotivoPendenciaDocumento`; | |
DELIMITER // | |
CREATE FUNCTION `getMotivoPendenciaDocumento`( | |
tipo_entidade VARCHAR(20), | |
entidade_id INT(11), | |
documento_tipo_id INT(11)) | |
RETURNS VARCHAR(20) | |
BEGIN | |
DECLARE documento_id INT(11); | |
DECLARE possui_documento TINYINT(1); | |
SET documento_id = ( | |
SELECT DISTINCT documentos.id | |
FROM documentos | |
WHERE documentos.deletado = 0 AND | |
(CASE | |
WHEN tipo_entidade = 'motorista' AND documentos.motorista_id = entidade_id THEN 1 | |
WHEN tipo_entidade = 'veiculo' AND documentos.veiculo_id = entidade_id THEN 1 | |
WHEN tipo_entidade = 'implemento' AND documentos.implemento_id = entidade_id THEN 1 | |
ELSE 0 | |
END) = 1 AND | |
documentos.documento_tipo_id = documento_tipo_id | |
ORDER BY documentos.criado_em DESC LIMIT 1 | |
); | |
IF (documento_id IS NULL) | |
RETURN 'sem documento'; | |
END IF | |
END //; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment