2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-08-20 08:06:10 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`client_getFromPhone`(vPhone VARCHAR(255))
|
2024-01-15 11:31:03 +00:00
|
|
|
RETURNS int(11)
|
|
|
|
DETERMINISTIC
|
|
|
|
BEGIN
|
|
|
|
/**
|
|
|
|
* Searchs a customer associated to the phone number.
|
|
|
|
*
|
|
|
|
* @param vPhone The phone number
|
|
|
|
* @return The client id or %NULL if not found
|
|
|
|
*/
|
|
|
|
DECLARE vClient INT DEFAULT NULL;
|
|
|
|
|
2024-05-31 10:28:47 +00:00
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tClient
|
2024-01-15 11:31:03 +00:00
|
|
|
ENGINE = MEMORY
|
|
|
|
SELECT id clientFk
|
|
|
|
FROM `client`
|
|
|
|
WHERE phone = vPhone
|
|
|
|
OR mobile = vPhone
|
|
|
|
UNION
|
|
|
|
SELECT clientFk
|
|
|
|
FROM address
|
|
|
|
WHERE phone = vPhone
|
|
|
|
OR mobile = vPhone
|
|
|
|
UNION
|
|
|
|
SELECT clientFk
|
2024-05-31 10:28:47 +00:00
|
|
|
FROM clientContact
|
2024-01-15 11:31:03 +00:00
|
|
|
WHERE phone = vPhone;
|
|
|
|
|
|
|
|
SELECT t.clientFk INTO vClient
|
|
|
|
FROM tClient t
|
|
|
|
JOIN `client` c ON c.id = t.clientFk
|
|
|
|
WHERE c.isActive
|
2024-05-31 10:28:47 +00:00
|
|
|
AND c.salesPersonFk
|
2024-01-15 11:31:03 +00:00
|
|
|
LIMIT 1;
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE tClient;
|
|
|
|
|
|
|
|
RETURN vClient;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|