From 4bef3e9107375adbe80e844f71b9c5de8526b889 Mon Sep 17 00:00:00 2001 From: ivanm Date: Wed, 3 Jul 2024 17:09:10 +0200 Subject: [PATCH 1/3] refs #7531 Modify zone_getAddresses --- .../vn/procedures/zone_getAddresses.sql | 67 ++++++++++++------- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/db/routines/vn/procedures/zone_getAddresses.sql b/db/routines/vn/procedures/zone_getAddresses.sql index ce7b0204e..c3a21aa83 100644 --- a/db/routines/vn/procedures/zone_getAddresses.sql +++ b/db/routines/vn/procedures/zone_getAddresses.sql @@ -1,7 +1,8 @@ DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`zone_getAddresses`( vSelf INT, - vLanded DATE + vDated DATE, + vDepartment INT ) BEGIN /** @@ -12,6 +13,7 @@ BEGIN * * @param vSelf Id de zona * @param vDated Fecha de entrega + * @param vDepartment Departamento del trabajador * @return Un select */ CALL zone_getPostalCode(vSelf); @@ -22,34 +24,47 @@ BEGIN WHERE id NOT IN ( SELECT clientFk FROM vn.ticket - WHERE landed BETWEEN vLanded AND util.dayEnd(vLanded) + WHERE landed BETWEEN vDated AND util.dayEnd(vDated) ) + ), + hasTicketShippedToday AS ( + SELECT clientFk, + IF (COUNT(*) > 0, TRUE, FALSE) hasTicketShipped + FROM vn.ticket + WHERE shipped BETWEEN vDated AND util.dayEnd(vDated) + GROUP BY clientFk ) 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; + c.name, + c.phone, + bt.description, + c.salesPersonFk, + u.name username, + aai.invoiced, + cnb.lastShipped, + ht.hasTicketShipped + 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 + 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 + LEFT JOIN hasTicketShippedToday ht ON ht.clientFk = c.id + WHERE a.isActive + AND c.isActive + AND ct.code = 'normal' + AND bt.code <> 'worker' + AND d.id = vDepartment + GROUP BY c.id; DROP TEMPORARY TABLE tmp.zoneNodes; END$$ From 50e2c03d3e1a2caac3643d49ad2cf220c5050a04 Mon Sep 17 00:00:00 2001 From: ivanm Date: Tue, 16 Jul 2024 18:56:00 +0200 Subject: [PATCH 2/3] refs #7531 Requested modifications in redmine --- .../vn/procedures/zone_getAddresses.sql | 37 ++++++------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/db/routines/vn/procedures/zone_getAddresses.sql b/db/routines/vn/procedures/zone_getAddresses.sql index c3a21aa83..417b8c7a3 100644 --- a/db/routines/vn/procedures/zone_getAddresses.sql +++ b/db/routines/vn/procedures/zone_getAddresses.sql @@ -1,40 +1,28 @@ DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`zone_getAddresses`( vSelf INT, - vDated DATE, - vDepartment INT + vShipped DATE, + vDepartmentFk INT ) 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. + * vender producto para esa zona. * * @param vSelf Id de zona - * @param vDated Fecha de entrega - * @param vDepartment Departamento del trabajador + * @param vShipped Fecha de envio + * @param vDepartmentFk Id de departamento * @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 vDated AND util.dayEnd(vDated) - ) - ), - hasTicketShippedToday AS ( - SELECT clientFk, - IF (COUNT(*) > 0, TRUE, FALSE) hasTicketShipped + WITH clientWithTicket AS ( + SELECT clientFk FROM vn.ticket - WHERE shipped BETWEEN vDated AND util.dayEnd(vDated) - GROUP BY clientFk + WHERE shipped BETWEEN vShipped AND util.dayEnd(vShipped) ) - SELECT c.id clientFk, + SELECT c.id, c.name, c.phone, bt.description, @@ -42,12 +30,12 @@ BEGIN u.name username, aai.invoiced, cnb.lastShipped, - ht.hasTicketShipped + cwt.clientFk 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 - JOIN notHasTicket ON notHasTicket.id = c.id + LEFT JOIN clientWithTicket cwt ON cwt.clientFk = 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 @@ -58,12 +46,11 @@ BEGIN 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 - LEFT JOIN hasTicketShippedToday ht ON ht.clientFk = c.id WHERE a.isActive AND c.isActive AND ct.code = 'normal' AND bt.code <> 'worker' - AND d.id = vDepartment + AND d.id = vDepartmentFk GROUP BY c.id; DROP TEMPORARY TABLE tmp.zoneNodes; From a6e6f435f75a976cf517c5e11d7ef1a0a32d6ab2 Mon Sep 17 00:00:00 2001 From: ivanm Date: Thu, 18 Jul 2024 15:21:59 +0200 Subject: [PATCH 3/3] refs #7531 No assigned department --- db/routines/vn/procedures/zone_getAddresses.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/routines/vn/procedures/zone_getAddresses.sql b/db/routines/vn/procedures/zone_getAddresses.sql index 417b8c7a3..0622ece1b 100644 --- a/db/routines/vn/procedures/zone_getAddresses.sql +++ b/db/routines/vn/procedures/zone_getAddresses.sql @@ -50,7 +50,7 @@ BEGIN AND c.isActive AND ct.code = 'normal' AND bt.code <> 'worker' - AND d.id = vDepartmentFk + AND (d.id = vDepartmentFk OR NOT vDepartmentFk) GROUP BY c.id; DROP TEMPORARY TABLE tmp.zoneNodes;