vn-asterisk/sql/client-from-phone.sql

49 lines
1.2 KiB
MySQL
Raw Permalink Normal View History

2016-04-13 14:22:35 +00:00
/**
* Obtains a customer id from a phone number.
*
* @param v_phone The caller phone number
* @return The customer id or %NULL if customer not exists or is inactive
**/
DROP FUNCTION IF EXISTS pbx.clientFromPhone;
2016-04-13 14:22:35 +00:00
DELIMITER $$
CREATE FUNCTION pbx.clientFromPhone (vPhone VARCHAR(255))
RETURNS INT
DETERMINISTIC
2016-04-13 14:22:35 +00:00
BEGIN
DECLARE vClient INT DEFAULT NULL;
-- SET vPhone = vPhone COLLATE 'utf8_unicode_ci';
-- Searchs a customer associated to the phone number
2016-04-13 14:22:35 +00:00
DROP TEMPORARY TABLE IF EXISTS tmp.customer;
CREATE TEMPORARY TABLE tmp.customer
ENGINE = MEMORY
SELECT id_cliente customer
FROM vn2008.Clientes c
WHERE telefono = vPhone
OR movil = vPhone
2016-04-13 14:22:35 +00:00
UNION
SELECT id_cliente
FROM vn2008.Consignatarios
WHERE telefono = vPhone
OR movil = vPhone
2016-04-13 14:22:35 +00:00
UNION
SELECT r.id_cliente
FROM vn2008.Relaciones r
JOIN vn2008.Contactos c ON r.Id_Contacto = c.Id_Contacto
WHERE c.telefono = vPhone
OR c.movil = vPhone;
2016-04-24 11:23:34 +00:00
SELECT t.customer INTO vClient
2016-04-13 14:22:35 +00:00
FROM tmp.customer t
2016-04-24 11:23:34 +00:00
JOIN vn2008.Clientes c ON c.id_cliente = t.customer
2016-04-13 14:22:35 +00:00
WHERE c.activo
LIMIT 1;
2016-04-13 14:22:35 +00:00
DROP TEMPORARY TABLE tmp.customer;
RETURN vClient;
2016-04-13 14:22:35 +00:00
END$$
DELIMITER ;