2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-08-20 08:06:10 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`zone_getAddresses`(
|
2024-01-15 11:31:03 +00:00
|
|
|
vSelf INT,
|
2024-07-16 16:56:00 +00:00
|
|
|
vShipped DATE,
|
|
|
|
vDepartmentFk INT
|
2024-01-15 11:31:03 +00:00
|
|
|
)
|
|
|
|
BEGIN
|
|
|
|
/**
|
|
|
|
* Devuelve un listado de todos los clientes activos
|
|
|
|
* con consignatarios a los que se les puede
|
2024-07-16 16:56:00 +00:00
|
|
|
* vender producto para esa zona.
|
2024-01-15 11:31:03 +00:00
|
|
|
*
|
|
|
|
* @param vSelf Id de zona
|
2024-07-16 16:56:00 +00:00
|
|
|
* @param vShipped Fecha de envio
|
|
|
|
* @param vDepartmentFk Id de departamento
|
2024-01-15 11:31:03 +00:00
|
|
|
* @return Un select
|
|
|
|
*/
|
|
|
|
CALL zone_getPostalCode(vSelf);
|
|
|
|
|
2024-07-16 16:56:00 +00:00
|
|
|
WITH clientWithTicket AS (
|
|
|
|
SELECT clientFk
|
2024-07-03 15:09:10 +00:00
|
|
|
FROM vn.ticket
|
2024-07-16 16:56:00 +00:00
|
|
|
WHERE shipped BETWEEN vShipped AND util.dayEnd(vShipped)
|
2024-01-15 11:31:03 +00:00
|
|
|
)
|
2024-07-16 16:56:00 +00:00
|
|
|
SELECT c.id,
|
2024-07-03 15:09:10 +00:00
|
|
|
c.name,
|
|
|
|
c.phone,
|
|
|
|
bt.description,
|
|
|
|
c.salesPersonFk,
|
|
|
|
u.name username,
|
|
|
|
aai.invoiced,
|
|
|
|
cnb.lastShipped,
|
2024-07-16 16:56:00 +00:00
|
|
|
cwt.clientFk
|
2024-07-03 15:09:10 +00:00
|
|
|
FROM vn.client c
|
|
|
|
JOIN vn.worker w ON w.id = c.salesPersonFk
|
|
|
|
JOIN vn.workerDepartment wd ON wd.workerFk = w.id
|
|
|
|
JOIN vn.department d ON d.id = wd.departmentFk
|
2024-07-16 16:56:00 +00:00
|
|
|
LEFT JOIN clientWithTicket cwt ON cwt.clientFk = c.id
|
2024-07-03 15:09:10 +00:00
|
|
|
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'
|
2024-07-18 13:21:59 +00:00
|
|
|
AND (d.id = vDepartmentFk OR NOT vDepartmentFk)
|
2024-07-03 15:09:10 +00:00
|
|
|
GROUP BY c.id;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
DROP TEMPORARY TABLE tmp.zoneNodes;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|