From 460723bd7aaf4c6503c90126b01187adefeb6f5d Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 8 Jul 2024 10:08:04 +0200 Subject: [PATCH 01/16] refactor: refs #6453 order_confirmWithUser --- .../procedures/order_confirmWithUser.sql | 81 +++++++++---------- 1 file changed, 39 insertions(+), 42 deletions(-) diff --git a/db/routines/hedera/procedures/order_confirmWithUser.sql b/db/routines/hedera/procedures/order_confirmWithUser.sql index 9c932aaa1..d85eb7f71 100644 --- a/db/routines/hedera/procedures/order_confirmWithUser.sql +++ b/db/routines/hedera/procedures/order_confirmWithUser.sql @@ -28,11 +28,8 @@ BEGIN DECLARE vClientId INT; DECLARE vCompanyId INT; DECLARE vAgencyModeId INT; - DECLARE TICKET_FREE INT DEFAULT 2; DECLARE vCalc INT; DECLARE vIsLogifloraItem BOOL; - DECLARE vOldQuantity INT; - DECLARE vNewQuantity INT; DECLARE vIsTaxDataChecked BOOL; DECLARE cDates CURSOR FOR @@ -40,14 +37,15 @@ BEGIN FROM `order` o JOIN order_row r ON r.order_id = o.id LEFT JOIN tmp.zoneGetShipped zgs ON zgs.warehouseFk = r.warehouse_id - WHERE o.id = vSelf AND r.amount != 0 + WHERE o.id = vSelf + AND r.amount GROUP BY r.warehouse_id; DECLARE cRows CURSOR FOR SELECT r.id, r.item_id, i.name, r.amount, r.price, r.rate, i.isFloramondo FROM order_row r JOIN vn.item i ON i.id = r.item_id - WHERE r.amount != 0 + WHERE r.amount AND r.warehouse_id = vWarehouse AND r.order_id = vSelf ORDER BY r.rate DESC; @@ -62,10 +60,20 @@ BEGIN END; -- Carga los datos del pedido - SELECT o.date_send, o.address_id, o.note, a.clientFk, - o.company_id, o.agency_id, c.isTaxDataChecked - INTO vDelivery, vAddress, vNotes, vClientId, - vCompanyId, vAgencyModeId, vIsTaxDataChecked + SELECT o.date_send, + o.address_id, + o.note, + a.clientFk, + o.company_id, + o.agency_id, + c.isTaxDataChecked + INTO vDelivery, + vAddress, + vNotes, + vClientId, + vCompanyId, + vAgencyModeId, + vIsTaxDataChecked FROM hedera.`order` o JOIN vn.address a ON a.id = o.address_id JOIN vn.client c ON c.id = a.clientFk @@ -73,11 +81,11 @@ BEGIN -- Verifica si el cliente tiene los datos comprobados IF NOT vIsTaxDataChecked THEN - CALL util.throw ('clientNotVerified'); + CALL util.throw('clientNotVerified'); END IF; -- Carga las fechas de salida de cada almacen - CALL vn.zone_getShipped (vDelivery, vAddress, vAgencyModeId, FALSE); + CALL vn.zone_getShipped(vDelivery, vAddress, vAgencyModeId, FALSE); -- Trabajador que realiza la accion IF vUserId IS NULL THEN @@ -94,7 +102,7 @@ BEGIN FROM order_row WHERE order_id = vSelf AND amount > 0; IF NOT vOk THEN - CALL util.throw ('ORDER_EMPTY'); + CALL util.throw('ORDER_EMPTY'); END IF; -- Crea los tickets del pedido @@ -112,23 +120,22 @@ BEGIN END IF; -- Busca un ticket existente que coincida con los parametros - WITH tPrevia AS - (SELECT DISTINCT s.ticketFk + WITH tPrevia AS ( + SELECT DISTINCT s.ticketFk FROM vn.sale s JOIN vn.saleGroupDetail sgd ON sgd.saleFk = s.id JOIN vn.ticket t ON t.id = s.ticketFk WHERE t.shipped BETWEEN vShipment AND util.dayend(vShipment) - ) + ) SELECT t.id INTO vTicket FROM vn.ticket t JOIN vn.alertLevel al ON al.code = 'FREE' LEFT JOIN tPrevia tp ON tp.ticketFk = t.id LEFT JOIN vn.ticketState tls on tls.ticketFk = t.id - JOIN hedera.`order` o - ON o.address_id = t.addressFk - AND vWarehouse = t.warehouseFk - AND o.date_send = t.landed - AND DATE(t.shipped) = vShipment + JOIN hedera.`order` o ON o.address_id = t.addressFk + AND vWarehouse = t.warehouseFk + AND o.date_send = t.landed + AND DATE(t.shipped) = vShipment WHERE o.id = vSelf AND t.refFk IS NULL AND tp.ticketFk IS NULL @@ -136,11 +143,8 @@ BEGIN LIMIT 1; -- Crea el ticket en el caso de no existir uno adecuado - IF vTicket IS NULL - THEN - + IF vTicket IS NULL THEN SET vShipment = IFNULL(vShipment, util.VN_CURDATE()); - CALL vn.ticket_add( vClientId, vShipment, @@ -158,7 +162,7 @@ BEGIN INSERT INTO vn.ticketTracking SET ticketFk = vTicket, userFk = vUserId, - stateFk = TICKET_FREE; + stateFk = (SELECT id FROM vn.state WHERE code = 'FREE'); END IF; INSERT IGNORE INTO vn.orderTicket @@ -166,21 +170,17 @@ BEGIN ticketFk = vTicket; -- Añade las notas - - IF vNotes IS NOT NULL AND vNotes != '' - THEN + IF vNotes IS NOT NULL AND vNotes <> '' THEN INSERT INTO vn.ticketObservation SET ticketFk = vTicket, - observationTypeFk = 4 /* salesperson */ , + observationTypeFk = (SELECT id FROM vn.observationType WHERE code = 'salesPerson'), `description` = vNotes ON DUPLICATE KEY UPDATE `description` = CONCAT(VALUES(`description`),'. ', `description`); END IF; -- Añade los movimientos y sus componentes - OPEN cRows; - lRows: LOOP SET vDone = FALSE; FETCH cRows INTO vRowId, vItem, vConcept, vAmount, vPrice, vRate, vIsLogifloraItem; @@ -191,7 +191,7 @@ BEGIN SET vSale = NULL; - SELECT s.id, s.quantity INTO vSale, vOldQuantity + SELECT s.id INTO vSale FROM vn.sale s WHERE ticketFk = vTicket AND price = vPrice @@ -204,10 +204,6 @@ BEGIN SET quantity = quantity + vAmount, originalQuantity = quantity WHERE id = vSale; - - SELECT s.quantity INTO vNewQuantity - FROM vn.sale s - WHERE id = vSale; ELSE -- Obtiene el coste SELECT SUM(rc.`price`) valueSum INTO vPriceFixed @@ -236,7 +232,8 @@ BEGIN GROUP BY vSale, rc.componentFk; END IF; - UPDATE order_row SET Id_Movimiento = vSale + UPDATE order_row + SET Id_Movimiento = vSale WHERE id = vRowId; -- Inserta en putOrder si la compra es de Floramondo @@ -245,13 +242,13 @@ BEGIN SET @available := 0; - SELECT GREATEST(0,available) INTO @available + SELECT GREATEST(0, available) INTO @available FROM cache.availableNoRaids WHERE calc_id = vCalc AND item_id = vItem; UPDATE cache.availableNoRaids - SET available = GREATEST(0,available - vAmount) + SET available = GREATEST(0, available - vAmount) WHERE item_id = vItem AND calc_id = vCalc; @@ -283,13 +280,13 @@ BEGIN LIMIT 1; END IF; END LOOP; - CLOSE cRows; END LOOP; - CLOSE cDates; - UPDATE `order` SET confirmed = TRUE, confirm_date = util.VN_NOW() + UPDATE `order` + SET confirmed = TRUE, + confirm_date = util.VN_NOW() WHERE id = vSelf; COMMIT; -- 2.40.1 From b06832735260c32f1388512e15c00a02a7050fe5 Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 8 Jul 2024 10:49:39 +0200 Subject: [PATCH 02/16] refactor: refs #6453 order_confirmWithUser --- db/routines/hedera/procedures/order_confirmWithUser.sql | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/db/routines/hedera/procedures/order_confirmWithUser.sql b/db/routines/hedera/procedures/order_confirmWithUser.sql index d85eb7f71..3801e935b 100644 --- a/db/routines/hedera/procedures/order_confirmWithUser.sql +++ b/db/routines/hedera/procedures/order_confirmWithUser.sql @@ -97,18 +97,17 @@ BEGIN CALL order_checkEditable(vSelf); -- Check order is not empty - SELECT COUNT(*) > 0 INTO vOk - FROM order_row WHERE order_id = vSelf AND amount > 0; + FROM order_row + WHERE order_id = vSelf + AND amount > 0; IF NOT vOk THEN CALL util.throw('ORDER_EMPTY'); END IF; -- Crea los tickets del pedido - OPEN cDates; - lDates: LOOP SET vTicket = NULL; -- 2.40.1 From b75bdc07c29f78b248357f3c445277bdfd0009d0 Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 10 Jul 2024 08:10:34 +0200 Subject: [PATCH 03/16] refactor: refs #6453 order_confirmWithUser --- .../procedures/order_confirmWithUser.sql | 142 +++++++++--------- 1 file changed, 71 insertions(+), 71 deletions(-) diff --git a/db/routines/hedera/procedures/order_confirmWithUser.sql b/db/routines/hedera/procedures/order_confirmWithUser.sql index 3801e935b..76ff4e723 100644 --- a/db/routines/hedera/procedures/order_confirmWithUser.sql +++ b/db/routines/hedera/procedures/order_confirmWithUser.sql @@ -1,36 +1,40 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `hedera`.`order_confirmWithUser`(vSelf INT, vUserId INT) +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `hedera`.`order_confirmWithUser`( + vSelf INT, + vUserFk INT +) BEGIN /** - * Confirms an order, creating each of its tickets on the corresponding - * date, store and user. + * Confirms an order, creating each of its tickets on + * the corresponding date, store and user. * * @param vSelf The order identifier * @param vUser The user identifier */ - DECLARE vOk BOOL; - DECLARE vDone BOOL DEFAULT FALSE; - DECLARE vWarehouse INT; + DECLARE vIsOk BOOL; + DECLARE vDone BOOL; + DECLARE vWarehouseFk INT; DECLARE vShipment DATE; - DECLARE vTicket INT; + DECLARE vTicketFk INT; DECLARE vNotes VARCHAR(255); DECLARE vItem INT; DECLARE vConcept VARCHAR(30); DECLARE vAmount INT; DECLARE vPrice DECIMAL(10,2); - DECLARE vSale INT; + DECLARE vSaleFk INT; DECLARE vRate INT; - DECLARE vRowId INT; + DECLARE vRowFk INT; DECLARE vPriceFixed DECIMAL(10,2); DECLARE vDelivery DATE; - DECLARE vAddress INT; + DECLARE vAddressFk INT; DECLARE vIsConfirmed BOOL; - DECLARE vClientId INT; - DECLARE vCompanyId INT; - DECLARE vAgencyModeId INT; - DECLARE vCalc INT; + DECLARE vClientFk INT; + DECLARE vCompanyFk INT; + DECLARE vAgencyModeFk INT; + DECLARE vCalcFk INT; DECLARE vIsLogifloraItem BOOL; DECLARE vIsTaxDataChecked BOOL; + DECLARE vAvailable INT; DECLARE cDates CURSOR FOR SELECT zgs.shipped, r.warehouse_id @@ -46,12 +50,11 @@ BEGIN FROM order_row r JOIN vn.item i ON i.id = r.item_id WHERE r.amount - AND r.warehouse_id = vWarehouse + AND r.warehouse_id = vWarehouseFk AND r.order_id = vSelf ORDER BY r.rate DESC; - DECLARE CONTINUE HANDLER FOR NOT FOUND - SET vDone = TRUE; + DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE; DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN @@ -68,13 +71,13 @@ BEGIN o.agency_id, c.isTaxDataChecked INTO vDelivery, - vAddress, + vAddressFk, vNotes, - vClientId, - vCompanyId, - vAgencyModeId, + vClientFk, + vCompanyFk, + vAgencyModeFk, vIsTaxDataChecked - FROM hedera.`order` o + FROM `order` o JOIN vn.address a ON a.id = o.address_id JOIN vn.client c ON c.id = a.clientFk WHERE o.id = vSelf; @@ -85,11 +88,11 @@ BEGIN END IF; -- Carga las fechas de salida de cada almacen - CALL vn.zone_getShipped(vDelivery, vAddress, vAgencyModeId, FALSE); + CALL vn.zone_getShipped(vDelivery, vAddressFk, vAgencyModeFk, FALSE); -- Trabajador que realiza la accion - IF vUserId IS NULL THEN - SELECT employeeFk INTO vUserId FROM orderConfig; + IF vUserFk IS NULL THEN + SELECT employeeFk INTO vUserFk FROM orderConfig; END IF; START TRANSACTION; @@ -97,12 +100,12 @@ BEGIN CALL order_checkEditable(vSelf); -- Check order is not empty - SELECT COUNT(*) > 0 INTO vOk + SELECT COUNT(*) > 0 INTO vIsOk FROM order_row WHERE order_id = vSelf AND amount > 0; - IF NOT vOk THEN + IF NOT vIsOk THEN CALL util.throw('ORDER_EMPTY'); END IF; @@ -110,9 +113,9 @@ BEGIN OPEN cDates; lDates: LOOP - SET vTicket = NULL; + SET vTicketFk = NULL; SET vDone = FALSE; - FETCH cDates INTO vShipment, vWarehouse; + FETCH cDates INTO vShipment, vWarehouseFk; IF vDone THEN LEAVE lDates; @@ -126,13 +129,13 @@ BEGIN JOIN vn.ticket t ON t.id = s.ticketFk WHERE t.shipped BETWEEN vShipment AND util.dayend(vShipment) ) - SELECT t.id INTO vTicket + SELECT t.id INTO vTicketFk FROM vn.ticket t JOIN vn.alertLevel al ON al.code = 'FREE' LEFT JOIN tPrevia tp ON tp.ticketFk = t.id LEFT JOIN vn.ticketState tls on tls.ticketFk = t.id JOIN hedera.`order` o ON o.address_id = t.addressFk - AND vWarehouse = t.warehouseFk + AND vWarehouseFk = t.warehouseFk AND o.date_send = t.landed AND DATE(t.shipped) = vShipment WHERE o.id = vSelf @@ -142,36 +145,36 @@ BEGIN LIMIT 1; -- Crea el ticket en el caso de no existir uno adecuado - IF vTicket IS NULL THEN + IF vTicketFk IS NULL THEN SET vShipment = IFNULL(vShipment, util.VN_CURDATE()); CALL vn.ticket_add( - vClientId, + vClientFk, vShipment, - vWarehouse, - vCompanyId, - vAddress, - vAgencyModeId, + vWarehouseFk, + vCompanyFk, + vAddressFk, + vAgencyModeFk, NULL, vDelivery, - vUserId, + vUserFk, TRUE, - vTicket + vTicketFk ); ELSE INSERT INTO vn.ticketTracking - SET ticketFk = vTicket, - userFk = vUserId, + SET ticketFk = vTicketFk, + userFk = vUserFk, stateFk = (SELECT id FROM vn.state WHERE code = 'FREE'); END IF; INSERT IGNORE INTO vn.orderTicket SET orderFk = vSelf, - ticketFk = vTicket; + ticketFk = vTicketFk; -- Añade las notas IF vNotes IS NOT NULL AND vNotes <> '' THEN INSERT INTO vn.ticketObservation SET - ticketFk = vTicket, + ticketFk = vTicketFk, observationTypeFk = (SELECT id FROM vn.observationType WHERE code = 'salesPerson'), `description` = vNotes ON DUPLICATE KEY UPDATE @@ -182,74 +185,71 @@ BEGIN OPEN cRows; lRows: LOOP SET vDone = FALSE; - FETCH cRows INTO vRowId, vItem, vConcept, vAmount, vPrice, vRate, vIsLogifloraItem; + FETCH cRows INTO vRowFk, vItem, vConcept, vAmount, vPrice, vRate, vIsLogifloraItem; IF vDone THEN LEAVE lRows; END IF; - SET vSale = NULL; + SET vSaleFk = NULL; - SELECT s.id INTO vSale + SELECT s.id INTO vSaleFk FROM vn.sale s - WHERE ticketFk = vTicket + WHERE ticketFk = vTicketFk AND price = vPrice AND itemFk = vItem AND discount = 0 LIMIT 1; - IF vSale THEN + IF vSaleFk THEN UPDATE vn.sale SET quantity = quantity + vAmount, originalQuantity = quantity - WHERE id = vSale; + WHERE id = vSaleFk; ELSE -- Obtiene el coste SELECT SUM(rc.`price`) valueSum INTO vPriceFixed FROM orderRowComponent rc JOIN vn.component c ON c.id = rc.componentFk JOIN vn.componentType ct ON ct.id = c.typeFk AND ct.isBase - WHERE rc.rowFk = vRowId; + WHERE rc.rowFk = vRowFk; INSERT INTO vn.sale SET itemFk = vItem, - ticketFk = vTicket, + ticketFk = vTicketFk, concept = vConcept, quantity = vAmount, price = vPrice, priceFixed = vPriceFixed, isPriceFixed = TRUE; - SET vSale = LAST_INSERT_ID(); + SET vSaleFk = LAST_INSERT_ID(); - INSERT INTO vn.saleComponent - (saleFk, componentFk, `value`) - SELECT vSale, rc.componentFk, rc.price + INSERT INTO vn.saleComponent (saleFk, componentFk, `value`) + SELECT vSaleFk, rc.componentFk, rc.price FROM orderRowComponent rc JOIN vn.component c ON c.id = rc.componentFk - WHERE rc.rowFk = vRowId - GROUP BY vSale, rc.componentFk; + WHERE rc.rowFk = vRowFk + GROUP BY vSaleFk, rc.componentFk; END IF; UPDATE order_row - SET Id_Movimiento = vSale - WHERE id = vRowId; + SET Id_Movimiento = vSaleFk + WHERE id = vRowFk; -- Inserta en putOrder si la compra es de Floramondo IF vIsLogifloraItem THEN - CALL cache.availableNoRaids_refresh(vCalc,FALSE,vWarehouse,vShipment); + CALL cache.availableNoRaids_refresh(vCalcFk, FALSE,vWarehouseFk, vShipment); - SET @available := 0; - - SELECT GREATEST(0, available) INTO @available + SELECT GREATEST(0, available) INTO vAvailable FROM cache.availableNoRaids - WHERE calc_id = vCalc + WHERE calc_id = vCalcFk AND item_id = vItem; UPDATE cache.availableNoRaids SET available = GREATEST(0, available - vAmount) WHERE item_id = vItem - AND calc_id = vCalc; + AND calc_id = vCalcFk; INSERT INTO edi.putOrder ( deliveryInformationID, @@ -262,20 +262,20 @@ BEGIN ) SELECT di.ID, i.supplyResponseFk, - CEIL((vAmount - @available)/ sr.NumberOfItemsPerCask), + CEIL((vAmount - vAvailable)/ sr.NumberOfItemsPerCask), o.address_id , - vClientId, + vClientFk, IFNULL(ca.fhAdminNumber, fhc.defaultAdminNumber), - vSale + vSaleFk FROM edi.deliveryInformation di JOIN vn.item i ON i.supplyResponseFk = di.supplyResponseID JOIN edi.supplyResponse sr ON sr.ID = i.supplyResponseFk - LEFT JOIN edi.clientFHAdminNumber ca ON ca.clientFk = vClientId + LEFT JOIN edi.clientFHAdminNumber ca ON ca.clientFk = vClientFk JOIN edi.floraHollandConfig fhc - JOIN hedera.`order` o ON o.id = vSelf + JOIN `order` o ON o.id = vSelf WHERE i.id = vItem AND di.LatestOrderDateTime > util.VN_NOW() - AND vAmount > @available + AND vAmount > vAvailable LIMIT 1; END IF; END LOOP; -- 2.40.1 From a8d03450732bd24292696ca17ba73c5efe703586 Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 10 Jul 2024 08:18:33 +0200 Subject: [PATCH 04/16] refactor: refs #6453 order_confirmWithUser --- .../procedures/order_confirmWithUser.sql | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/db/routines/hedera/procedures/order_confirmWithUser.sql b/db/routines/hedera/procedures/order_confirmWithUser.sql index 76ff4e723..61a7421cf 100644 --- a/db/routines/hedera/procedures/order_confirmWithUser.sql +++ b/db/routines/hedera/procedures/order_confirmWithUser.sql @@ -17,7 +17,7 @@ BEGIN DECLARE vShipment DATE; DECLARE vTicketFk INT; DECLARE vNotes VARCHAR(255); - DECLARE vItem INT; + DECLARE vItemFk INT; DECLARE vConcept VARCHAR(30); DECLARE vAmount INT; DECLARE vPrice DECIMAL(10,2); @@ -46,12 +46,12 @@ BEGIN GROUP BY r.warehouse_id; DECLARE cRows CURSOR FOR - SELECT r.id, r.item_id, i.name, r.amount, r.price, r.rate, i.isFloramondo - FROM order_row r - JOIN vn.item i ON i.id = r.item_id + SELECT r.id, r.itemFk, i.name, r.amount, r.price, r.rate, i.isFloramondo + FROM orderRow r + JOIN vn.item i ON i.id = r.itemFk WHERE r.amount - AND r.warehouse_id = vWarehouseFk - AND r.order_id = vSelf + AND r.warehouseFk = vWarehouseFk + AND r.orderFk = vSelf ORDER BY r.rate DESC; DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE; @@ -185,7 +185,7 @@ BEGIN OPEN cRows; lRows: LOOP SET vDone = FALSE; - FETCH cRows INTO vRowFk, vItem, vConcept, vAmount, vPrice, vRate, vIsLogifloraItem; + FETCH cRows INTO vRowFk, vItemFk, vConcept, vAmount, vPrice, vRate, vIsLogifloraItem; IF vDone THEN LEAVE lRows; @@ -197,7 +197,7 @@ BEGIN FROM vn.sale s WHERE ticketFk = vTicketFk AND price = vPrice - AND itemFk = vItem + AND itemFk = vItemFk AND discount = 0 LIMIT 1; @@ -215,7 +215,7 @@ BEGIN WHERE rc.rowFk = vRowFk; INSERT INTO vn.sale - SET itemFk = vItem, + SET itemFk = vItemFk, ticketFk = vTicketFk, concept = vConcept, quantity = vAmount, @@ -244,11 +244,11 @@ BEGIN SELECT GREATEST(0, available) INTO vAvailable FROM cache.availableNoRaids WHERE calc_id = vCalcFk - AND item_id = vItem; + AND item_id = vItemFk; UPDATE cache.availableNoRaids SET available = GREATEST(0, available - vAmount) - WHERE item_id = vItem + WHERE item_id = vItemFk AND calc_id = vCalcFk; INSERT INTO edi.putOrder ( @@ -273,7 +273,7 @@ BEGIN LEFT JOIN edi.clientFHAdminNumber ca ON ca.clientFk = vClientFk JOIN edi.floraHollandConfig fhc JOIN `order` o ON o.id = vSelf - WHERE i.id = vItem + WHERE i.id = vItemFk AND di.LatestOrderDateTime > util.VN_NOW() AND vAmount > vAvailable LIMIT 1; -- 2.40.1 From 0e760ba5057b97b55e97c9d1c3bd6a5481eed5dd Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 10 Jul 2024 08:23:43 +0200 Subject: [PATCH 05/16] refactor: refs #6453 order_confirmWithUser --- .../hedera/procedures/order_confirmWithUser.sql | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/db/routines/hedera/procedures/order_confirmWithUser.sql b/db/routines/hedera/procedures/order_confirmWithUser.sql index 61a7421cf..b5962ec88 100644 --- a/db/routines/hedera/procedures/order_confirmWithUser.sql +++ b/db/routines/hedera/procedures/order_confirmWithUser.sql @@ -37,13 +37,13 @@ BEGIN DECLARE vAvailable INT; DECLARE cDates CURSOR FOR - SELECT zgs.shipped, r.warehouse_id + SELECT zgs.shipped, r.warehouseFk FROM `order` o - JOIN order_row r ON r.order_id = o.id - LEFT JOIN tmp.zoneGetShipped zgs ON zgs.warehouseFk = r.warehouse_id + JOIN orderRow r ON r.orderFk = o.id + LEFT JOIN tmp.zoneGetShipped zgs ON zgs.warehouseFk = r.warehouseFk WHERE o.id = vSelf AND r.amount - GROUP BY r.warehouse_id; + GROUP BY r.warehouseFk; DECLARE cRows CURSOR FOR SELECT r.id, r.itemFk, i.name, r.amount, r.price, r.rate, i.isFloramondo @@ -101,8 +101,8 @@ BEGIN -- Check order is not empty SELECT COUNT(*) > 0 INTO vIsOk - FROM order_row - WHERE order_id = vSelf + FROM orderRow + WHERE orderFk = vSelf AND amount > 0; IF NOT vIsOk THEN @@ -233,8 +233,8 @@ BEGIN GROUP BY vSaleFk, rc.componentFk; END IF; - UPDATE order_row - SET Id_Movimiento = vSaleFk + UPDATE orderRow + SET saleFk = vSaleFk WHERE id = vRowFk; -- Inserta en putOrder si la compra es de Floramondo -- 2.40.1 From a1d3d2f2f8ea12ef313e705cf372bf4ceae82674 Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 10 Jul 2024 10:30:24 +0200 Subject: [PATCH 06/16] refactor: refs #6453 Major changes --- .../procedures/order_confirmWithUser.sql | 214 ++++++++++++------ 1 file changed, 141 insertions(+), 73 deletions(-) diff --git a/db/routines/hedera/procedures/order_confirmWithUser.sql b/db/routines/hedera/procedures/order_confirmWithUser.sql index b5962ec88..bf4408fbc 100644 --- a/db/routines/hedera/procedures/order_confirmWithUser.sql +++ b/db/routines/hedera/procedures/order_confirmWithUser.sql @@ -5,8 +5,8 @@ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `hedera`.`order_confirmWi ) BEGIN /** - * Confirms an order, creating each of its tickets on - * the corresponding date, store and user. + * Confirms an order, creating each of its tickets + * on the corresponding date, store and user. * * @param vSelf The order identifier * @param vUser The user identifier @@ -25,7 +25,7 @@ BEGIN DECLARE vRate INT; DECLARE vRowFk INT; DECLARE vPriceFixed DECIMAL(10,2); - DECLARE vDelivery DATE; + DECLARE vLanded DATE; DECLARE vAddressFk INT; DECLARE vIsConfirmed BOOL; DECLARE vClientFk INT; @@ -35,8 +35,9 @@ BEGIN DECLARE vIsLogifloraItem BOOL; DECLARE vIsTaxDataChecked BOOL; DECLARE vAvailable INT; + DECLARE vItemPackingTypeFk VARCHAR(1); - DECLARE cDates CURSOR FOR + DECLARE vDates CURSOR FOR SELECT zgs.shipped, r.warehouseFk FROM `order` o JOIN orderRow r ON r.orderFk = o.id @@ -45,8 +46,25 @@ BEGIN AND r.amount GROUP BY r.warehouseFk; - DECLARE cRows CURSOR FOR - SELECT r.id, r.itemFk, i.name, r.amount, r.price, r.rate, i.isFloramondo + DECLARE vDistinctItemPackingType CURSOR FOR + SELECT DISTINCT i.itemPackingTypeFk + FROM `order` o + JOIN orderRow r ON r.orderFk = o.id + JOIN vn.item i ON i.id = r.itemFk + WHERE o.id = vSelf + AND r.warehouseFk = vWarehouseFk + AND r.amount + ORDER BY i.itemPackingTypeFk DESC; + + DECLARE vRows CURSOR FOR + SELECT r.id, + r.itemFk, + i.name, + r.amount, + r.price, + r.rate, + i.itemPackingTypeFk, + i.isFloramondo FROM orderRow r JOIN vn.item i ON i.id = r.itemFk WHERE r.amount @@ -70,7 +88,7 @@ BEGIN o.company_id, o.agency_id, c.isTaxDataChecked - INTO vDelivery, + INTO vLanded, vAddressFk, vNotes, vClientFk, @@ -88,7 +106,7 @@ BEGIN END IF; -- Carga las fechas de salida de cada almacen - CALL vn.zone_getShipped(vDelivery, vAddressFk, vAgencyModeFk, FALSE); + CALL vn.zone_getShipped(vLanded, vAddressFk, vAgencyModeFk, FALSE); -- Trabajador que realiza la accion IF vUserFk IS NULL THEN @@ -110,88 +128,135 @@ BEGIN END IF; -- Crea los tickets del pedido - OPEN cDates; - lDates: - LOOP + OPEN vDates; + lDates: LOOP SET vTicketFk = NULL; SET vDone = FALSE; - FETCH cDates INTO vShipment, vWarehouseFk; + FETCH vDates INTO vShipment, vWarehouseFk; IF vDone THEN LEAVE lDates; END IF; + CREATE OR REPLACE TEMPORARY TABLE tTicketByItemPackingType( + itemPackingTypeFk VARCHAR(1), + ticketFk INT, + PRIMARY KEY(itemPackingTypeFk, ticketFk) + ) ENGINE = MEMORY; + -- Busca un ticket existente que coincida con los parametros - WITH tPrevia AS ( - SELECT DISTINCT s.ticketFk - FROM vn.sale s - JOIN vn.saleGroupDetail sgd ON sgd.saleFk = s.id - JOIN vn.ticket t ON t.id = s.ticketFk - WHERE t.shipped BETWEEN vShipment AND util.dayend(vShipment) - ) - SELECT t.id INTO vTicketFk - FROM vn.ticket t - JOIN vn.alertLevel al ON al.code = 'FREE' - LEFT JOIN tPrevia tp ON tp.ticketFk = t.id - LEFT JOIN vn.ticketState tls on tls.ticketFk = t.id - JOIN hedera.`order` o ON o.address_id = t.addressFk - AND vWarehouseFk = t.warehouseFk - AND o.date_send = t.landed - AND DATE(t.shipped) = vShipment - WHERE o.id = vSelf - AND t.refFk IS NULL - AND tp.ticketFk IS NULL - AND (tls.alertLevel IS NULL OR tls.alertLevel = al.id) - LIMIT 1; + OPEN vDistinctItemPackingType; + lDistinctItemPackingType: LOOP + SET vItemPackingTypeFk = NULL; + SET vDone = FALSE; + FETCH vDistinctItemPackingType INTO vItemPackingTypeFk; - -- Crea el ticket en el caso de no existir uno adecuado - IF vTicketFk IS NULL THEN - SET vShipment = IFNULL(vShipment, util.VN_CURDATE()); - CALL vn.ticket_add( - vClientFk, - vShipment, - vWarehouseFk, - vCompanyFk, - vAddressFk, - vAgencyModeFk, - NULL, - vDelivery, - vUserFk, - TRUE, - vTicketFk - ); - ELSE - INSERT INTO vn.ticketTracking - SET ticketFk = vTicketFk, - userFk = vUserFk, - stateFk = (SELECT id FROM vn.state WHERE code = 'FREE'); - END IF; + IF vDone THEN + LEAVE lDistinctItemPackingType; + END IF; - INSERT IGNORE INTO vn.orderTicket - SET orderFk = vSelf, - ticketFk = vTicketFk; + WITH tPrevia AS ( + SELECT DISTINCT s.ticketFk + FROM vn.sale s + JOIN vn.saleGroupDetail sgd ON sgd.saleFk = s.id + JOIN vn.ticket t ON t.id = s.ticketFk + WHERE t.shipped BETWEEN vShipment AND util.dayend(vShipment) + ), + tTicketSameItemPackingType AS ( + SELECT t.id, COUNT(*) = SUM(IF( + vItemPackingTypeFk IS NOT NULL, + i.itemPackingTypeFk = vItemPackingTypeFk, + i.itemPackingTypeFk IS NULL + )) hasSameItemPackingType + FROM ticket t + JOIN sale s ON s.ticketFk = t.id + JOIN item i ON i.id = s.itemFk + WHERE t.shipped = vShipment + GROUP BY t.id + HAVING hasSameItemPackingType + ) + SELECT t.id INTO vTicketFk + FROM vn.ticket t + JOIN vn.alertLevel al ON al.code = 'FREE' + LEFT JOIN tPrevia tp ON tp.ticketFk = t.id + LEFT JOIN vn.ticketState tls on tls.ticketFk = t.id + JOIN hedera.`order` o ON o.address_id = t.addressFk + AND t.warehouseFk = vWarehouseFk + AND o.date_send = t.landed + AND DATE(t.shipped) = vShipment + JOIN tTicketSameItemPackingType tt ON tt.id = t.id + WHERE o.id = vSelf + AND t.refFk IS NULL + AND tp.ticketFk IS NULL + AND (tls.alertLevel IS NULL OR tls.alertLevel = al.id) + LIMIT 1; - -- Añade las notas - IF vNotes IS NOT NULL AND vNotes <> '' THEN - INSERT INTO vn.ticketObservation SET - ticketFk = vTicketFk, - observationTypeFk = (SELECT id FROM vn.observationType WHERE code = 'salesPerson'), - `description` = vNotes - ON DUPLICATE KEY UPDATE - `description` = CONCAT(VALUES(`description`),'. ', `description`); - END IF; + -- Crea el ticket en el caso de no existir uno adecuado + IF vTicketFk IS NULL THEN + SET vShipment = IFNULL(vShipment, util.VN_CURDATE()); + CALL vn.ticket_add( + vClientFk, + vShipment, + vWarehouseFk, + vCompanyFk, + vAddressFk, + vAgencyModeFk, + NULL, + vLanded, + vUserFk, + TRUE, + vTicketFk + ); + ELSE + INSERT INTO vn.ticketTracking + SET ticketFk = vTicketFk, + userFk = vUserFk, + stateFk = (SELECT id FROM vn.state WHERE code = 'FREE'); + END IF; + + INSERT IGNORE INTO vn.orderTicket + SET orderFk = vSelf, + ticketFk = vTicketFk; + + -- Añade las notas + IF vNotes IS NOT NULL AND vNotes <> '' THEN + INSERT INTO vn.ticketObservation SET + ticketFk = vTicketFk, + observationTypeFk = (SELECT id FROM vn.observationType WHERE code = 'salesPerson'), + `description` = vNotes + ON DUPLICATE KEY UPDATE + `description` = CONCAT(VALUES(`description`),'. ', `description`); + END IF; + + INSERT INTO tTicketByItemPackingType + SET itemPackingTypeFk = vItemPackingTypeFk, + ticketFk = vTicketFk; + END LOOP; + CLOSE vDistinctItemPackingType; -- Añade los movimientos y sus componentes - OPEN cRows; + OPEN vRows; lRows: LOOP + SET vSaleFk = NULL; SET vDone = FALSE; - FETCH cRows INTO vRowFk, vItemFk, vConcept, vAmount, vPrice, vRate, vIsLogifloraItem; + FETCH vRows INTO vRowFk, + vItemFk, + vConcept, + vAmount, + vPrice, + vRate, + vItemPackingTypeFk, + vIsLogifloraItem; IF vDone THEN LEAVE lRows; END IF; - SET vSaleFk = NULL; + SELECT ticketFk INTO vTicketFk + FROM tTicketByItemPackingType + WHERE IF(vItemPackingTypeFk IS NOT NULL, + itemPackingTypeFk = vItemPackingTypeFk, + itemPackingTypeFk IS NULL) SELECT s.id INTO vSaleFk FROM vn.sale s @@ -211,7 +276,8 @@ BEGIN SELECT SUM(rc.`price`) valueSum INTO vPriceFixed FROM orderRowComponent rc JOIN vn.component c ON c.id = rc.componentFk - JOIN vn.componentType ct ON ct.id = c.typeFk AND ct.isBase + JOIN vn.componentType ct ON ct.id = c.typeFk + AND ct.isBase WHERE rc.rowFk = vRowFk; INSERT INTO vn.sale @@ -279,9 +345,9 @@ BEGIN LIMIT 1; END IF; END LOOP; - CLOSE cRows; + CLOSE vRows; END LOOP; - CLOSE cDates; + CLOSE vDates; UPDATE `order` SET confirmed = TRUE, @@ -289,5 +355,7 @@ BEGIN WHERE id = vSelf; COMMIT; + + DROP TEMPORARY TABLE tTicketByItemPackingType; END$$ DELIMITER ; -- 2.40.1 From 7e415181c2afb40421ca3bf109533ea674c9606b Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 10 Jul 2024 11:26:45 +0200 Subject: [PATCH 07/16] refactor: refs #6453 Major changes --- .../procedures/order_confirmWithUser.sql | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/db/routines/hedera/procedures/order_confirmWithUser.sql b/db/routines/hedera/procedures/order_confirmWithUser.sql index bf4408fbc..246648e18 100644 --- a/db/routines/hedera/procedures/order_confirmWithUser.sql +++ b/db/routines/hedera/procedures/order_confirmWithUser.sql @@ -36,6 +36,7 @@ BEGIN DECLARE vIsTaxDataChecked BOOL; DECLARE vAvailable INT; DECLARE vItemPackingTypeFk VARCHAR(1); + DECLARE vCountDistinctItemPackingTypeFk INT; DECLARE vDates CURSOR FOR SELECT zgs.shipped, r.warehouseFk @@ -54,7 +55,7 @@ BEGIN WHERE o.id = vSelf AND r.warehouseFk = vWarehouseFk AND r.amount - ORDER BY i.itemPackingTypeFk DESC; + ORDER BY i.itemPackingTypeFk DESC; -- El último siempre NULL!! DECLARE vRows CURSOR FOR SELECT r.id, @@ -155,6 +156,28 @@ BEGIN LEAVE lDistinctItemPackingType; END IF; + IF vItemPackingTypeFk IS NULL THEN + SELECT COUNT(*) INTO vCountDistinctItemPackingTypeFk + FROM tTicketByItemPackingType; + + CASE + WHEN vCountDistinctItemPackingTypeFk = 1 THEN + INSERT INTO tTicketByItemPackingType + SET itemPackingTypeFk = vItemPackingTypeFk, + ticketFk = (SELECT ticketFk FROM tTicketByItemPackingType); + LEAVE lDistinctItemPackingType; + WHEN vCountDistinctItemPackingTypeFk > 1 THEN + INSERT INTO tTicketByItemPackingType + SET itemPackingTypeFk = vItemPackingTypeFk, + ticketFk = ( + SELECT ticketFk + FROM tTicketByItemPackingType + WHERE itemPackingTypeFk = 'H' + ); + LEAVE lDistinctItemPackingType; + END CASE; + END IF; + WITH tPrevia AS ( SELECT DISTINCT s.ticketFk FROM vn.sale s -- 2.40.1 From 15f580508bf3e4cfcb32bd63b49847c8e8e6ba98 Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 10 Jul 2024 11:29:14 +0200 Subject: [PATCH 08/16] refactor: refs #6453 Minor changes --- db/routines/hedera/procedures/order_confirmWithUser.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/db/routines/hedera/procedures/order_confirmWithUser.sql b/db/routines/hedera/procedures/order_confirmWithUser.sql index 246648e18..fe0f33b3f 100644 --- a/db/routines/hedera/procedures/order_confirmWithUser.sql +++ b/db/routines/hedera/procedures/order_confirmWithUser.sql @@ -55,7 +55,8 @@ BEGIN WHERE o.id = vSelf AND r.warehouseFk = vWarehouseFk AND r.amount - ORDER BY i.itemPackingTypeFk DESC; -- El último siempre NULL!! + ORDER BY i.itemPackingTypeFk DESC; + -- El último siempre NULL, es imprescindible para la lógica !!! DECLARE vRows CURSOR FOR SELECT r.id, -- 2.40.1 From 6dc464d26b15de4f83815ff01a44bce620f74233 Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 11 Jul 2024 07:14:02 +0200 Subject: [PATCH 09/16] refactor: refs #6453 Minor changes --- db/routines/hedera/procedures/order_confirmWithUser.sql | 3 +++ 1 file changed, 3 insertions(+) diff --git a/db/routines/hedera/procedures/order_confirmWithUser.sql b/db/routines/hedera/procedures/order_confirmWithUser.sql index fe0f33b3f..c48b74dd7 100644 --- a/db/routines/hedera/procedures/order_confirmWithUser.sql +++ b/db/routines/hedera/procedures/order_confirmWithUser.sql @@ -162,6 +162,8 @@ BEGIN FROM tTicketByItemPackingType; CASE + WHEN NOT vCountDistinctItemPackingTypeFk THEN + -- Code WHEN vCountDistinctItemPackingTypeFk = 1 THEN INSERT INTO tTicketByItemPackingType SET itemPackingTypeFk = vItemPackingTypeFk, @@ -196,6 +198,7 @@ BEGIN JOIN sale s ON s.ticketFk = t.id JOIN item i ON i.id = s.itemFk WHERE t.shipped = vShipment + AND t.warehouseFk= vWarehouseFk GROUP BY t.id HAVING hasSameItemPackingType ) -- 2.40.1 From 5f29384389c741e2e0d79cde820eb992a026a75d Mon Sep 17 00:00:00 2001 From: guillermo Date: Fri, 19 Jul 2024 10:49:45 +0200 Subject: [PATCH 10/16] feat: #6453 Rollback always split by itemPackingType --- .../procedures/order_confirmWithUser.sql | 190 +++++------------- 1 file changed, 55 insertions(+), 135 deletions(-) diff --git a/db/routines/hedera/procedures/order_confirmWithUser.sql b/db/routines/hedera/procedures/order_confirmWithUser.sql index c48b74dd7..86489b080 100644 --- a/db/routines/hedera/procedures/order_confirmWithUser.sql +++ b/db/routines/hedera/procedures/order_confirmWithUser.sql @@ -47,17 +47,6 @@ BEGIN AND r.amount GROUP BY r.warehouseFk; - DECLARE vDistinctItemPackingType CURSOR FOR - SELECT DISTINCT i.itemPackingTypeFk - FROM `order` o - JOIN orderRow r ON r.orderFk = o.id - JOIN vn.item i ON i.id = r.itemFk - WHERE o.id = vSelf - AND r.warehouseFk = vWarehouseFk - AND r.amount - ORDER BY i.itemPackingTypeFk DESC; - -- El último siempre NULL, es imprescindible para la lógica !!! - DECLARE vRows CURSOR FOR SELECT r.id, r.itemFk, @@ -140,126 +129,65 @@ BEGIN LEAVE lDates; END IF; - CREATE OR REPLACE TEMPORARY TABLE tTicketByItemPackingType( - itemPackingTypeFk VARCHAR(1), - ticketFk INT, - PRIMARY KEY(itemPackingTypeFk, ticketFk) - ) ENGINE = MEMORY; - -- Busca un ticket existente que coincida con los parametros - OPEN vDistinctItemPackingType; - lDistinctItemPackingType: LOOP - SET vItemPackingTypeFk = NULL; - SET vDone = FALSE; - FETCH vDistinctItemPackingType INTO vItemPackingTypeFk; + WITH tPrevia AS ( + SELECT DISTINCT s.ticketFk + FROM vn.sale s + JOIN vn.saleGroupDetail sgd ON sgd.saleFk = s.id + JOIN vn.ticket t ON t.id = s.ticketFk + WHERE t.shipped BETWEEN vShipment AND util.dayend(vShipment) + ) + SELECT t.id INTO vTicketFk + FROM vn.ticket t + JOIN vn.alertLevel al ON al.code = 'FREE' + LEFT JOIN tPrevia tp ON tp.ticketFk = t.id + LEFT JOIN vn.ticketState tls on tls.ticketFk = t.id + JOIN hedera.`order` o ON o.address_id = t.addressFk + AND t.warehouseFk = vWarehouseFk + AND o.date_send = t.landed + AND DATE(t.shipped) = vShipment + WHERE o.id = vSelf + AND t.refFk IS NULL + AND tp.ticketFk IS NULL + AND (tls.alertLevel IS NULL OR tls.alertLevel = al.id) + LIMIT 1; - IF vDone THEN - LEAVE lDistinctItemPackingType; - END IF; + -- Crea el ticket en el caso de no existir uno adecuado + IF vTicketFk IS NULL THEN + SET vShipment = IFNULL(vShipment, util.VN_CURDATE()); + CALL vn.ticket_add( + vClientFk, + vShipment, + vWarehouseFk, + vCompanyFk, + vAddressFk, + vAgencyModeFk, + NULL, + vLanded, + vUserFk, + TRUE, + vTicketFk + ); + ELSE + INSERT INTO vn.ticketTracking + SET ticketFk = vTicketFk, + userFk = vUserFk, + stateFk = (SELECT id FROM vn.state WHERE code = 'FREE'); + END IF; - IF vItemPackingTypeFk IS NULL THEN - SELECT COUNT(*) INTO vCountDistinctItemPackingTypeFk - FROM tTicketByItemPackingType; + INSERT IGNORE INTO vn.orderTicket + SET orderFk = vSelf, + ticketFk = vTicketFk; - CASE - WHEN NOT vCountDistinctItemPackingTypeFk THEN - -- Code - WHEN vCountDistinctItemPackingTypeFk = 1 THEN - INSERT INTO tTicketByItemPackingType - SET itemPackingTypeFk = vItemPackingTypeFk, - ticketFk = (SELECT ticketFk FROM tTicketByItemPackingType); - LEAVE lDistinctItemPackingType; - WHEN vCountDistinctItemPackingTypeFk > 1 THEN - INSERT INTO tTicketByItemPackingType - SET itemPackingTypeFk = vItemPackingTypeFk, - ticketFk = ( - SELECT ticketFk - FROM tTicketByItemPackingType - WHERE itemPackingTypeFk = 'H' - ); - LEAVE lDistinctItemPackingType; - END CASE; - END IF; - - WITH tPrevia AS ( - SELECT DISTINCT s.ticketFk - FROM vn.sale s - JOIN vn.saleGroupDetail sgd ON sgd.saleFk = s.id - JOIN vn.ticket t ON t.id = s.ticketFk - WHERE t.shipped BETWEEN vShipment AND util.dayend(vShipment) - ), - tTicketSameItemPackingType AS ( - SELECT t.id, COUNT(*) = SUM(IF( - vItemPackingTypeFk IS NOT NULL, - i.itemPackingTypeFk = vItemPackingTypeFk, - i.itemPackingTypeFk IS NULL - )) hasSameItemPackingType - FROM ticket t - JOIN sale s ON s.ticketFk = t.id - JOIN item i ON i.id = s.itemFk - WHERE t.shipped = vShipment - AND t.warehouseFk= vWarehouseFk - GROUP BY t.id - HAVING hasSameItemPackingType - ) - SELECT t.id INTO vTicketFk - FROM vn.ticket t - JOIN vn.alertLevel al ON al.code = 'FREE' - LEFT JOIN tPrevia tp ON tp.ticketFk = t.id - LEFT JOIN vn.ticketState tls on tls.ticketFk = t.id - JOIN hedera.`order` o ON o.address_id = t.addressFk - AND t.warehouseFk = vWarehouseFk - AND o.date_send = t.landed - AND DATE(t.shipped) = vShipment - JOIN tTicketSameItemPackingType tt ON tt.id = t.id - WHERE o.id = vSelf - AND t.refFk IS NULL - AND tp.ticketFk IS NULL - AND (tls.alertLevel IS NULL OR tls.alertLevel = al.id) - LIMIT 1; - - -- Crea el ticket en el caso de no existir uno adecuado - IF vTicketFk IS NULL THEN - SET vShipment = IFNULL(vShipment, util.VN_CURDATE()); - CALL vn.ticket_add( - vClientFk, - vShipment, - vWarehouseFk, - vCompanyFk, - vAddressFk, - vAgencyModeFk, - NULL, - vLanded, - vUserFk, - TRUE, - vTicketFk - ); - ELSE - INSERT INTO vn.ticketTracking - SET ticketFk = vTicketFk, - userFk = vUserFk, - stateFk = (SELECT id FROM vn.state WHERE code = 'FREE'); - END IF; - - INSERT IGNORE INTO vn.orderTicket - SET orderFk = vSelf, - ticketFk = vTicketFk; - - -- Añade las notas - IF vNotes IS NOT NULL AND vNotes <> '' THEN - INSERT INTO vn.ticketObservation SET - ticketFk = vTicketFk, - observationTypeFk = (SELECT id FROM vn.observationType WHERE code = 'salesPerson'), - `description` = vNotes - ON DUPLICATE KEY UPDATE - `description` = CONCAT(VALUES(`description`),'. ', `description`); - END IF; - - INSERT INTO tTicketByItemPackingType - SET itemPackingTypeFk = vItemPackingTypeFk, - ticketFk = vTicketFk; - END LOOP; - CLOSE vDistinctItemPackingType; + -- Añade las notas + IF vNotes IS NOT NULL AND vNotes <> '' THEN + INSERT INTO vn.ticketObservation SET + ticketFk = vTicketFk, + observationTypeFk = (SELECT id FROM vn.observationType WHERE code = 'salesPerson'), + `description` = vNotes + ON DUPLICATE KEY UPDATE + `description` = CONCAT(VALUES(`description`),'. ', `description`); + END IF; -- Añade los movimientos y sus componentes OPEN vRows; @@ -279,12 +207,6 @@ BEGIN LEAVE lRows; END IF; - SELECT ticketFk INTO vTicketFk - FROM tTicketByItemPackingType - WHERE IF(vItemPackingTypeFk IS NOT NULL, - itemPackingTypeFk = vItemPackingTypeFk, - itemPackingTypeFk IS NULL) - SELECT s.id INTO vSaleFk FROM vn.sale s WHERE ticketFk = vTicketFk @@ -382,7 +304,5 @@ BEGIN WHERE id = vSelf; COMMIT; - - DROP TEMPORARY TABLE tTicketByItemPackingType; END$$ DELIMITER ; -- 2.40.1 From 578de51f7e689b7ddc9ab03d5bf8015eb2c181f5 Mon Sep 17 00:00:00 2001 From: guillermo Date: Fri, 19 Jul 2024 10:54:40 +0200 Subject: [PATCH 11/16] feat: #6453 Refactor --- db/routines/hedera/procedures/order_confirmWithUser.sql | 7 ------- 1 file changed, 7 deletions(-) diff --git a/db/routines/hedera/procedures/order_confirmWithUser.sql b/db/routines/hedera/procedures/order_confirmWithUser.sql index 86489b080..841a69390 100644 --- a/db/routines/hedera/procedures/order_confirmWithUser.sql +++ b/db/routines/hedera/procedures/order_confirmWithUser.sql @@ -22,21 +22,16 @@ BEGIN DECLARE vAmount INT; DECLARE vPrice DECIMAL(10,2); DECLARE vSaleFk INT; - DECLARE vRate INT; DECLARE vRowFk INT; DECLARE vPriceFixed DECIMAL(10,2); DECLARE vLanded DATE; DECLARE vAddressFk INT; - DECLARE vIsConfirmed BOOL; DECLARE vClientFk INT; DECLARE vCompanyFk INT; DECLARE vAgencyModeFk INT; DECLARE vCalcFk INT; DECLARE vIsLogifloraItem BOOL; DECLARE vIsTaxDataChecked BOOL; - DECLARE vAvailable INT; - DECLARE vItemPackingTypeFk VARCHAR(1); - DECLARE vCountDistinctItemPackingTypeFk INT; DECLARE vDates CURSOR FOR SELECT zgs.shipped, r.warehouseFk @@ -53,7 +48,6 @@ BEGIN i.name, r.amount, r.price, - r.rate, i.itemPackingTypeFk, i.isFloramondo FROM orderRow r @@ -199,7 +193,6 @@ BEGIN vConcept, vAmount, vPrice, - vRate, vItemPackingTypeFk, vIsLogifloraItem; -- 2.40.1 From 4c602800ea9183a72ef358e29a5141286711bb5c Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 29 Jul 2024 11:46:59 +0200 Subject: [PATCH 12/16] feat: refs #6453 Added new ticket search --- .../procedures/order_confirmWithUser.sql | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/db/routines/hedera/procedures/order_confirmWithUser.sql b/db/routines/hedera/procedures/order_confirmWithUser.sql index 841a69390..48f97c9b6 100644 --- a/db/routines/hedera/procedures/order_confirmWithUser.sql +++ b/db/routines/hedera/procedures/order_confirmWithUser.sql @@ -123,14 +123,14 @@ BEGIN LEAVE lDates; END IF; - -- Busca un ticket existente que coincida con los parametros + -- Busca un ticket libre disponible WITH tPrevia AS ( SELECT DISTINCT s.ticketFk FROM vn.sale s JOIN vn.saleGroupDetail sgd ON sgd.saleFk = s.id JOIN vn.ticket t ON t.id = s.ticketFk WHERE t.shipped BETWEEN vShipment AND util.dayend(vShipment) - ) + ), SELECT t.id INTO vTicketFk FROM vn.ticket t JOIN vn.alertLevel al ON al.code = 'FREE' @@ -146,6 +146,37 @@ BEGIN AND (tls.alertLevel IS NULL OR tls.alertLevel = al.id) LIMIT 1; + -- Comprobamos si hay un ticket de previa disponible + IF vTicketFk IS NULL THEN + WITH tItemPackingTypeOrder AS ( + SELECT GROUP_CONCAT(DISTINCT i.itemPackingTypeFk) distinctItemPackingTypes, + o.address_id + FROM vn.item i + JOIN orderRow oro ON oro.itemFk = i.id + JOIN `order` o ON o.id = oro.orderFk + WHERE oro.orderFk = vSelf + ), + tItemPackingTypeTicket AS ( + SELECT t.id, + GROUP_CONCAT(DISTINCT i.itemPackingTypeFk) distinctItemPackingTypes + FROM vn.ticket t + JOIN vn.alertLevel al ON al.code = 'ON_PREVIOUS' + JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.item i ON i.id = s.itemFk + JOIN tItemPackingTypeOrder ipto + WHERE t.refFk IS NULL + AND DATE(t.shipped) = vShipment + AND t.warehouseFk = vWarehouseFk + AND t.addressFk = ipto.address_id + GROUP BY t.id + ) + SELECT iptt.id INTO vTicketFk + FROM tItemPackingTypeTicket iptt + JOIN tItemPackingTypeOrder ipto + WHERE INSTR(iptt.distinctItemPackingTypes, ipto.distinctItemPackingTypes) + LIMIT 1 + END IF; + -- Crea el ticket en el caso de no existir uno adecuado IF vTicketFk IS NULL THEN SET vShipment = IFNULL(vShipment, util.VN_CURDATE()); -- 2.40.1 From e53b12d51bc220990d3e90ea0f20a2985adec303 Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 29 Jul 2024 12:31:44 +0200 Subject: [PATCH 13/16] feat: refs #6453 Fixes --- db/routines/hedera/procedures/order_confirmWithUser.sql | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/db/routines/hedera/procedures/order_confirmWithUser.sql b/db/routines/hedera/procedures/order_confirmWithUser.sql index 48f97c9b6..cf0f9aead 100644 --- a/db/routines/hedera/procedures/order_confirmWithUser.sql +++ b/db/routines/hedera/procedures/order_confirmWithUser.sql @@ -20,6 +20,7 @@ BEGIN DECLARE vItemFk INT; DECLARE vConcept VARCHAR(30); DECLARE vAmount INT; + DECLARE vAvailable INT; DECLARE vPrice DECIMAL(10,2); DECLARE vSaleFk INT; DECLARE vRowFk INT; @@ -48,7 +49,6 @@ BEGIN i.name, r.amount, r.price, - i.itemPackingTypeFk, i.isFloramondo FROM orderRow r JOIN vn.item i ON i.id = r.itemFk @@ -130,7 +130,7 @@ BEGIN JOIN vn.saleGroupDetail sgd ON sgd.saleFk = s.id JOIN vn.ticket t ON t.id = s.ticketFk WHERE t.shipped BETWEEN vShipment AND util.dayend(vShipment) - ), + ) SELECT t.id INTO vTicketFk FROM vn.ticket t JOIN vn.alertLevel al ON al.code = 'FREE' @@ -174,7 +174,7 @@ BEGIN FROM tItemPackingTypeTicket iptt JOIN tItemPackingTypeOrder ipto WHERE INSTR(iptt.distinctItemPackingTypes, ipto.distinctItemPackingTypes) - LIMIT 1 + LIMIT 1; END IF; -- Crea el ticket en el caso de no existir uno adecuado @@ -224,7 +224,6 @@ BEGIN vConcept, vAmount, vPrice, - vItemPackingTypeFk, vIsLogifloraItem; IF vDone THEN -- 2.40.1 From 6ad3d5a5ebdc37873d5c6dc3791e6529fe1308bb Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 29 Jul 2024 12:36:08 +0200 Subject: [PATCH 14/16] feat: refs #6453 Minor changes --- db/routines/hedera/procedures/order_confirmWithUser.sql | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/db/routines/hedera/procedures/order_confirmWithUser.sql b/db/routines/hedera/procedures/order_confirmWithUser.sql index cf0f9aead..4fcee91f7 100644 --- a/db/routines/hedera/procedures/order_confirmWithUser.sql +++ b/db/routines/hedera/procedures/order_confirmWithUser.sql @@ -15,6 +15,7 @@ BEGIN DECLARE vDone BOOL; DECLARE vWarehouseFk INT; DECLARE vShipment DATE; + DECLARE vShipmentDayEnd DATE; DECLARE vTicketFk INT; DECLARE vNotes VARCHAR(255); DECLARE vItemFk INT; @@ -123,13 +124,15 @@ BEGIN LEAVE lDates; END IF; + SET vShipmentDayEnd = util.dayEnd(vShipment); + -- Busca un ticket libre disponible WITH tPrevia AS ( SELECT DISTINCT s.ticketFk FROM vn.sale s JOIN vn.saleGroupDetail sgd ON sgd.saleFk = s.id JOIN vn.ticket t ON t.id = s.ticketFk - WHERE t.shipped BETWEEN vShipment AND util.dayend(vShipment) + WHERE t.shipped BETWEEN vShipment AND vShipmentDayEnd ) SELECT t.id INTO vTicketFk FROM vn.ticket t @@ -139,7 +142,7 @@ BEGIN JOIN hedera.`order` o ON o.address_id = t.addressFk AND t.warehouseFk = vWarehouseFk AND o.date_send = t.landed - AND DATE(t.shipped) = vShipment + AND t.shipped BETWEEN vShipment AND vShipmentDayEnd WHERE o.id = vSelf AND t.refFk IS NULL AND tp.ticketFk IS NULL @@ -165,7 +168,7 @@ BEGIN JOIN vn.item i ON i.id = s.itemFk JOIN tItemPackingTypeOrder ipto WHERE t.refFk IS NULL - AND DATE(t.shipped) = vShipment + AND t.shipped BETWEEN vShipment AND vShipmentDayEnd AND t.warehouseFk = vWarehouseFk AND t.addressFk = ipto.address_id GROUP BY t.id -- 2.40.1 From b6abcbe0907eb78e28feab73800ac5abebb064e0 Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 30 Jul 2024 10:14:20 +0200 Subject: [PATCH 15/16] feat: refs #6453 Requested changes --- .../procedures/order_confirmWithUser.sql | 65 +++---------------- 1 file changed, 8 insertions(+), 57 deletions(-) diff --git a/db/routines/hedera/procedures/order_confirmWithUser.sql b/db/routines/hedera/procedures/order_confirmWithUser.sql index 4fcee91f7..17798caf8 100644 --- a/db/routines/hedera/procedures/order_confirmWithUser.sql +++ b/db/routines/hedera/procedures/order_confirmWithUser.sql @@ -11,11 +11,11 @@ BEGIN * @param vSelf The order identifier * @param vUser The user identifier */ - DECLARE vIsOk BOOL; + DECLARE vHasRows BOOL; DECLARE vDone BOOL; DECLARE vWarehouseFk INT; DECLARE vShipment DATE; - DECLARE vShipmentDayEnd DATE; + DECLARE vShipmentDayEnd DATETIME; DECLARE vTicketFk INT; DECLARE vNotes VARCHAR(255); DECLARE vItemFk INT; @@ -32,7 +32,6 @@ BEGIN DECLARE vCompanyFk INT; DECLARE vAgencyModeFk INT; DECLARE vCalcFk INT; - DECLARE vIsLogifloraItem BOOL; DECLARE vIsTaxDataChecked BOOL; DECLARE vDates CURSOR FOR @@ -49,8 +48,7 @@ BEGIN r.itemFk, i.name, r.amount, - r.price, - i.isFloramondo + r.price FROM orderRow r JOIN vn.item i ON i.id = r.itemFk WHERE r.amount @@ -104,12 +102,12 @@ BEGIN CALL order_checkEditable(vSelf); -- Check order is not empty - SELECT COUNT(*) > 0 INTO vIsOk + SELECT COUNT(*) > 0 INTO vHasRows FROM orderRow WHERE orderFk = vSelf AND amount > 0; - IF NOT vIsOk THEN + IF NOT vHasRows THEN CALL util.throw('ORDER_EMPTY'); END IF; @@ -209,8 +207,8 @@ BEGIN -- Añade las notas IF vNotes IS NOT NULL AND vNotes <> '' THEN - INSERT INTO vn.ticketObservation SET - ticketFk = vTicketFk, + INSERT INTO vn.ticketObservation + SET ticketFk = vTicketFk, observationTypeFk = (SELECT id FROM vn.observationType WHERE code = 'salesPerson'), `description` = vNotes ON DUPLICATE KEY UPDATE @@ -222,12 +220,7 @@ BEGIN lRows: LOOP SET vSaleFk = NULL; SET vDone = FALSE; - FETCH vRows INTO vRowFk, - vItemFk, - vConcept, - vAmount, - vPrice, - vIsLogifloraItem; + FETCH vRows INTO vRowFk, vItemFk, vConcept, vAmount, vPrice; IF vDone THEN LEAVE lRows; @@ -277,48 +270,6 @@ BEGIN UPDATE orderRow SET saleFk = vSaleFk WHERE id = vRowFk; - - -- Inserta en putOrder si la compra es de Floramondo - IF vIsLogifloraItem THEN - CALL cache.availableNoRaids_refresh(vCalcFk, FALSE,vWarehouseFk, vShipment); - - SELECT GREATEST(0, available) INTO vAvailable - FROM cache.availableNoRaids - WHERE calc_id = vCalcFk - AND item_id = vItemFk; - - UPDATE cache.availableNoRaids - SET available = GREATEST(0, available - vAmount) - WHERE item_id = vItemFk - AND calc_id = vCalcFk; - - INSERT INTO edi.putOrder ( - deliveryInformationID, - supplyResponseId, - quantity , - EndUserPartyId, - EndUserPartyGLN, - FHAdminNumber, - saleFk - ) - SELECT di.ID, - i.supplyResponseFk, - CEIL((vAmount - vAvailable)/ sr.NumberOfItemsPerCask), - o.address_id , - vClientFk, - IFNULL(ca.fhAdminNumber, fhc.defaultAdminNumber), - vSaleFk - FROM edi.deliveryInformation di - JOIN vn.item i ON i.supplyResponseFk = di.supplyResponseID - JOIN edi.supplyResponse sr ON sr.ID = i.supplyResponseFk - LEFT JOIN edi.clientFHAdminNumber ca ON ca.clientFk = vClientFk - JOIN edi.floraHollandConfig fhc - JOIN `order` o ON o.id = vSelf - WHERE i.id = vItemFk - AND di.LatestOrderDateTime > util.VN_NOW() - AND vAmount > vAvailable - LIMIT 1; - END IF; END LOOP; CLOSE vRows; END LOOP; -- 2.40.1 From b56260af365a0a2097b6068a80340ec56c27b202 Mon Sep 17 00:00:00 2001 From: guillermo Date: Fri, 2 Aug 2024 07:58:45 +0200 Subject: [PATCH 16/16] feat: refs #6453 Requested changes --- .../procedures/order_confirmWithUser.sql | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/db/routines/hedera/procedures/order_confirmWithUser.sql b/db/routines/hedera/procedures/order_confirmWithUser.sql index 17798caf8..3acdaed5a 100644 --- a/db/routines/hedera/procedures/order_confirmWithUser.sql +++ b/db/routines/hedera/procedures/order_confirmWithUser.sql @@ -136,11 +136,11 @@ BEGIN FROM vn.ticket t JOIN vn.alertLevel al ON al.code = 'FREE' LEFT JOIN tPrevia tp ON tp.ticketFk = t.id - LEFT JOIN vn.ticketState tls on tls.ticketFk = t.id + LEFT JOIN vn.ticketState tls ON tls.ticketFk = t.id JOIN hedera.`order` o ON o.address_id = t.addressFk + AND t.shipped BETWEEN vShipment AND vShipmentDayEnd AND t.warehouseFk = vWarehouseFk AND o.date_send = t.landed - AND t.shipped BETWEEN vShipment AND vShipmentDayEnd WHERE o.id = vSelf AND t.refFk IS NULL AND tp.ticketFk IS NULL @@ -150,7 +150,9 @@ BEGIN -- Comprobamos si hay un ticket de previa disponible IF vTicketFk IS NULL THEN WITH tItemPackingTypeOrder AS ( - SELECT GROUP_CONCAT(DISTINCT i.itemPackingTypeFk) distinctItemPackingTypes, + SELECT GROUP_CONCAT( + DISTINCT i.itemPackingTypeFk ORDER BY i.itemPackingTypeFk + ) distinctItemPackingTypes, o.address_id FROM vn.item i JOIN orderRow oro ON oro.itemFk = i.id @@ -159,16 +161,20 @@ BEGIN ), tItemPackingTypeTicket AS ( SELECT t.id, - GROUP_CONCAT(DISTINCT i.itemPackingTypeFk) distinctItemPackingTypes + GROUP_CONCAT( + DISTINCT i.itemPackingTypeFk ORDER BY i.itemPackingTypeFk + ) distinctItemPackingTypes FROM vn.ticket t - JOIN vn.alertLevel al ON al.code = 'ON_PREVIOUS' + JOIN vn.ticketState tls ON tls.ticketFk = t.id + JOIN vn.alertLevel al ON al.id = tls.alertLevel JOIN vn.sale s ON s.ticketFk = t.id JOIN vn.item i ON i.id = s.itemFk JOIN tItemPackingTypeOrder ipto - WHERE t.refFk IS NULL - AND t.shipped BETWEEN vShipment AND vShipmentDayEnd + WHERE t.shipped BETWEEN vShipment AND vShipmentDayEnd + AND t.refFk IS NULL AND t.warehouseFk = vWarehouseFk AND t.addressFk = ipto.address_id + AND al.code = 'ON_PREVIOUS' GROUP BY t.id ) SELECT iptt.id INTO vTicketFk -- 2.40.1