DELIMITER $$
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`machineWorker_add`(vPlate VARCHAR(10), vWorkerFk INT)
BEGIN
	
/**
 * Inserta registro si el vWorkerFk no ha registrado nada en las últimas 12 horas
 * @param vPlate número de matrícula
 * @param vWorkerFk id del worker 
 * 
*/    
		UPDATE vn.machineWorker mw 
	    		JOIN vn.machine m ON m.id = mw.machineFk 
    		SET mw.outTimed = util.VN_NOW()
	    	WHERE (mw.workerFk = vWorkerFk OR m.plate = vPlate)
	    		AND ISNULL(mw.outTimed);
	    	
    	INSERT INTO machineWorker (machineFk, workerFk)
	        SELECT m.id, vWorkerFk
	        	FROM machine m
	            WHERE m.plate= vPlate;	     
END$$
DELIMITER ;
