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;