2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-07-22 09:52:42 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`client_unassignSalesPerson`()
|
2024-01-15 11:31:03 +00:00
|
|
|
BEGIN
|
|
|
|
/**
|
|
|
|
* Elimina la asignación de salesPersonFk de la ficha del clientes
|
|
|
|
* que no han realizado una compra en los últimos 3 meses y reparte
|
|
|
|
* su greuge entre el resto de clientes
|
|
|
|
*/
|
|
|
|
DECLARE vDone BOOL DEFAULT FALSE;
|
|
|
|
DECLARE vClientFk INT;
|
|
|
|
DECLARE vCursor CURSOR FOR
|
|
|
|
SELECT c.clientFk
|
|
|
|
FROM tClientList c
|
|
|
|
LEFT JOIN clientRisk r ON r.clientFk = c.clientFk
|
|
|
|
GROUP BY c.clientFk
|
|
|
|
HAVING NOT SUM(IFNULL(r.amount, 0));
|
|
|
|
|
|
|
|
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
|
|
|
|
|
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tClientList
|
2024-01-25 16:33:54 +00:00
|
|
|
SELECT c.id clientFk
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM bs.clientDied cd
|
2024-01-25 16:33:54 +00:00
|
|
|
JOIN client c ON c.id = cd.clientFk
|
2024-01-15 11:31:03 +00:00
|
|
|
LEFT JOIN clientProtected cp ON cp.clientFk = c.id
|
|
|
|
LEFT JOIN salesPersonProtected sp ON sp.salesPersonFk = c.salesPersonFk
|
|
|
|
JOIN province p ON p.id = c.provinceFk
|
|
|
|
LEFT JOIN autonomy a ON a.id = p.autonomyFk
|
|
|
|
JOIN country co ON co.id = p.countryFk
|
2024-06-26 07:54:51 +00:00
|
|
|
JOIN bs.clientDiedPeriod cdp ON cdp.countryFk = co.id
|
2024-01-15 11:31:03 +00:00
|
|
|
WHERE cd.warning = 'third'
|
|
|
|
AND cp.clientFk IS NULL
|
|
|
|
AND sp.salesPersonFk IS NULL
|
|
|
|
AND a.name <> 'Canarias'
|
|
|
|
AND c.salesPersonFk IS NOT NULL;
|
|
|
|
|
|
|
|
OPEN vCursor;
|
|
|
|
l: LOOP
|
|
|
|
SET vDone = FALSE;
|
|
|
|
FETCH vCursor INTO vClientFk;
|
|
|
|
IF vDone THEN
|
|
|
|
LEAVE l;
|
|
|
|
END IF;
|
|
|
|
CALL clientGreugeSpray(vClientFk, TRUE, '', TRUE);
|
|
|
|
UPDATE client
|
|
|
|
SET salesPersonFk = NULL
|
|
|
|
WHERE id = vClientFk;
|
|
|
|
END LOOP;
|
|
|
|
CLOSE vCursor;
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE tClientList;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|