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
|
|
|
|
**/
|
2018-08-02 11:43:57 +00:00
|
|
|
DROP FUNCTION IF EXISTS pbx.clientFromPhone;
|
2016-04-13 14:22:35 +00:00
|
|
|
DELIMITER $$
|
2018-08-02 11:43:57 +00:00
|
|
|
CREATE FUNCTION pbx.clientFromPhone (vPhone VARCHAR(255))
|
|
|
|
RETURNS INT
|
|
|
|
DETERMINISTIC
|
2016-04-13 14:22:35 +00:00
|
|
|
BEGIN
|
2018-08-02 11:43:57 +00:00
|
|
|
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
|
2018-08-02 11:43:57 +00:00
|
|
|
WHERE telefono = vPhone
|
|
|
|
OR movil = vPhone
|
2016-04-13 14:22:35 +00:00
|
|
|
UNION
|
|
|
|
SELECT id_cliente
|
|
|
|
FROM vn2008.Consignatarios
|
2018-08-02 11:43:57 +00:00
|
|
|
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
|
2018-08-02 11:43:57 +00:00
|
|
|
WHERE c.telefono = vPhone
|
|
|
|
OR c.movil = vPhone;
|
2016-04-24 11:23:34 +00:00
|
|
|
|
2018-08-02 11:43:57 +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
|
2018-08-02 11:43:57 +00:00
|
|
|
LIMIT 1;
|
2016-04-13 14:22:35 +00:00
|
|
|
|
|
|
|
DROP TEMPORARY TABLE tmp.customer;
|
|
|
|
|
2018-08-02 11:43:57 +00:00
|
|
|
RETURN vClient;
|
2016-04-13 14:22:35 +00:00
|
|
|
END$$
|
|
|
|
DELIMITER ;
|