2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`zone_getAddresses`(
|
|
|
|
vSelf INT,
|
|
|
|
vLanded DATE
|
|
|
|
)
|
|
|
|
BEGIN
|
|
|
|
/**
|
|
|
|
* Devuelve un listado de todos los clientes activos
|
|
|
|
* con consignatarios a los que se les puede
|
|
|
|
* vender producto para esa zona y no tiene un ticket
|
|
|
|
* para ese día.
|
|
|
|
*
|
|
|
|
* @param vSelf Id de zona
|
|
|
|
* @param vDated Fecha de entrega
|
|
|
|
* @return Un select
|
|
|
|
*/
|
|
|
|
CALL zone_getPostalCode(vSelf);
|
|
|
|
|
|
|
|
WITH notHasTicket AS (
|
|
|
|
SELECT id
|
|
|
|
FROM vn.client
|
|
|
|
WHERE id NOT IN (
|
|
|
|
SELECT clientFk
|
|
|
|
FROM vn.ticket
|
|
|
|
WHERE landed BETWEEN vLanded AND util.dayEnd(vLanded)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
SELECT c.id clientFk,
|
|
|
|
c.name,
|
|
|
|
c.phone,
|
2024-04-08 07:27:07 +00:00
|
|
|
bt.description,
|
2024-01-15 11:31:03 +00:00
|
|
|
c.salesPersonFk,
|
|
|
|
u.name username,
|
|
|
|
aai.invoiced,
|
|
|
|
cnb.lastShipped
|
|
|
|
FROM vn.client c
|
|
|
|
JOIN notHasTicket ON notHasTicket.id = c.id
|
|
|
|
LEFT JOIN account.`user` u ON u.id = c.salesPersonFk
|
|
|
|
JOIN vn.`address` a ON a.clientFk = c.id
|
|
|
|
JOIN vn.postCode pc ON pc.code = a.postalCode
|
|
|
|
JOIN vn.town t ON t.id = pc.townFk AND t.provinceFk = a.provinceFk
|
|
|
|
JOIN vn.zoneGeo zg ON zg.name = a.postalCode
|
|
|
|
JOIN tmp.zoneNodes zn ON zn.geoFk = pc.geoFk
|
|
|
|
LEFT JOIN bs.clientNewBorn cnb ON cnb.clientFk = c.id
|
|
|
|
LEFT JOIN vn.annualAverageInvoiced aai ON aai.clientFk = c.id
|
|
|
|
JOIN vn.clientType ct ON ct.code = c.typeFk
|
2024-04-08 07:27:07 +00:00
|
|
|
JOIN vn.businessType bt ON bt.code = c.businessTypeFk
|
2024-01-15 11:31:03 +00:00
|
|
|
WHERE a.isActive
|
|
|
|
AND c.isActive
|
|
|
|
AND ct.code = 'normal'
|
2024-04-08 07:27:07 +00:00
|
|
|
AND bt.code <> 'worker'
|
2024-01-15 11:31:03 +00:00
|
|
|
GROUP BY c.id;
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE tmp.zoneNodes;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|