57 lines
1.5 KiB
SQL
57 lines
1.5 KiB
SQL
DELIMITER $$
|
|
CREATE OR REPLACE DEFINER=`vn-admin`@`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,
|
|
bt.description,
|
|
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
|
|
JOIN vn.businessType bt ON bt.code = c.businessTypeFk
|
|
WHERE a.isActive
|
|
AND c.isActive
|
|
AND ct.code = 'normal'
|
|
AND bt.code <> 'worker'
|
|
GROUP BY c.id;
|
|
|
|
DROP TEMPORARY TABLE tmp.zoneNodes;
|
|
END$$
|
|
DELIMITER ;
|