DELIMITER $$ CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`conveyorExpedition_Add`(vStarted DATETIME, vEnded DATETIME) BEGIN DECLARE startingMinute INT; TRUNCATE vn.conveyorExpedition; INSERT INTO vn.conveyorExpedition( expeditionFk, created, length, width, height, routeFk, productionMinute, truck, criterion, conveyorBuildingClassFk) SELECT e.id, e.created, 10 * p.depth as depth, 10 * p.width as width, 10 * p.height as height, IFNULL(t.routeFk,am.agencyFk) routeFk, hour(e.created) * 60 + minute(e.created), IFNULL(rs.description , a.name), IFNULL(t.routeFk,am.agencyFk) criterion, IFNULL(p.conveyorBuildingClassFk , pc.defaultConveyorBuildingClass) FROM vn.expedition e JOIN vn.ticket t ON t.id = e.ticketFk JOIN vn.packaging p ON p.id = e.packagingFk LEFT JOIN vn.zone z ON z.id = t.zoneFk LEFT JOIN vn.agencyMode am ON am.id = z.agencyModeFk LEFT JOIN vn.agency a ON a.id = am.agencyFk LEFT JOIN vn.routesMonitor rm ON rm.routeFk = t.routeFk LEFT JOIN vn.roadmapStop rs ON rs.id = rm.roadmapStopFk JOIN vn.packagingConfig pc WHERE t.warehouseFk IN (60,1,44) AND e.created BETWEEN vStarted AND vEnded AND p.height < 140 AND p.width < 60 AND p.depth < 125; SELECT MIN(productionMinute) INTO startingMinute FROM vn.conveyorExpedition ce ; UPDATE vn.conveyorExpedition SET productionMinute = productionMinute - startingMinute; END$$ DELIMITER ;