DELIMITER $$ CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`ticket_DelayTruckSplit`( vTicketFk INT ) BEGIN /** * Splita las lineas de ticket que no estan ubicadas * * @param vTicketFk Id ticket */ DECLARE vNewTicketFk INT; DECLARE vTotalLines INT; DECLARE vLinesToSplit INT; DROP TEMPORARY TABLE IF EXISTS tmp.SalesToSplit; SELECT COUNT(*) INTO vTotalLines FROM sale WHERE ticketFk = vTicketFk; CREATE TEMPORARY TABLE tmp.SalesToSplit SELECT s.id saleFk FROM ticket t JOIN sale s ON t.id = s.ticketFk LEFT JOIN ( SELECT ish.itemFk itemFk, SUM(ish.visible) visible, s.warehouseFk warehouseFk FROM itemShelving ish JOIN shelving sh ON sh.code = ish.shelvingFk JOIN parking p ON p.id = sh.parkingFk JOIN sector s ON s.id = p.sectorFk GROUP BY ish.itemFk, s.warehouseFk ) issw ON issw.itemFk = s.itemFk AND issw.warehouseFk = t.warehouseFk WHERE s.quantity > IFNULL(issw.visible, 0) AND s.quantity > 0 AND NOT s.isPicked AND NOT s.reserved AND t.id = vTicketFk; SELECT COUNT(*) INTO vLinesToSplit FROM tmp.SalesToSplit; IF vLinesToSplit = vTotalLines AND vLinesToSplit > 0 THEN SET vNewTicketFk = vTicketFk; ELSE CALL ticket_Clone(vTicketFk, vNewTicketFk); UPDATE sale s JOIN tmp.SalesToSplit sts ON sts.saleFk = s.id SET s.ticketFk = vNewTicketFk; END IF; CALL ticket_setState(vNewTicketFk, 'FIXING'); DROP TEMPORARY TABLE tmp.SalesToSplit; END$$ DELIMITER ;