From 031c4398aea2607553126ecc1237384bd87398bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Andr=C3=A9s?= Date: Wed, 20 Nov 2024 19:19:03 +0100 Subject: [PATCH] refactor: refs #8210 ticket_splitItemPackingType --- .../ticket_splitItemPackingType.sql | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/db/routines/vn/procedures/ticket_splitItemPackingType.sql b/db/routines/vn/procedures/ticket_splitItemPackingType.sql index 1a1e1ff07..31e0c24e7 100644 --- a/db/routines/vn/procedures/ticket_splitItemPackingType.sql +++ b/db/routines/vn/procedures/ticket_splitItemPackingType.sql @@ -18,7 +18,7 @@ BEGIN DECLARE vDefaultPackingType VARCHAR(1); DECLARE vHasOriginalPackingType BOOLEAN; DECLARE vNewTicketFk INT; - DECLARE vThrowAway INT; + DECLARE vTicketFk INT; DECLARE vItemPackingTypes CURSOR FOR SELECT DISTINCT itemPackingTypeFk FROM tSalesToMove; @@ -37,15 +37,12 @@ BEGIN itemPackingTypeFk VARCHAR(1) ) ENGINE=MEMORY; - SELECT defaultPackingTypeFk INTO vDefaultPackingType FROM itemConfig LIMIT 1; - - START TRANSACTION; - - SELECT t.id INTO vThrowAway - FROM ticket t - JOIN sale s ON s.id = t.id - WHERE t.id = vSelf - FOR UPDATE; + SELECT COALESCE(MAX(ic.defaultPackingTypeFk), MAX(i.itemPackingTypeFk)) INTO vDefaultPackingType + FROM vn.sale s + JOIN item i ON i.id = s.itemFk + LEFT JOIN itemConfig ic ON ic.defaultPackingTypeFk = i.itemPackingTypeFk + WHERE s.ticketFk = vSelf + GROUP BY s.ticketFk; SELECT EXISTS ( SELECT TRUE @@ -59,6 +56,14 @@ BEGIN SET vOriginalItemPackingTypeFk = vDefaultPackingType; END IF; + START TRANSACTION; + + SELECT t.id INTO vTicketFk + FROM ticket t + JOIN sale s ON s.id = t.id + WHERE t.id = vSelf + FOR UPDATE; + INSERT INTO tSalesToMove (saleFk, itemPackingTypeFk) SELECT s.id, i.itemPackingTypeFk FROM sale s @@ -77,7 +82,7 @@ BEGIN CALL ticket_Clone(vSelf, vNewTicketFk); - SELECT id INTO vThrowAway + SELECT id INTO vTicketFk FROM ticket t WHERE t.id = vNewTicketFk FOR UPDATE;