DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`expeditionState_addByAdress`(vAdressFk INT, vRouteFk INT, vStateCode VARCHAR(100))
BEGIN

	/**
	 * Inserta nuevos registros en la tabla vn.expeditionState
	 *
	 * @param vAdressFk Identificador de vn.ticket
	 * @param vRouteFk Identificador de vn.route
	 * @param vStateCode Corresponde a vn.expeditionStateType.code
	 */

	INSERT INTO vn.expeditionState(expeditionFk, typeFk)
		SELECT e.id, est.id
			FROM vn.expedition e
				JOIN vn.expeditionStateType est ON est.code = vStateCode
				JOIN vn.ticket t ON t.id = e.ticketFk 
			WHERE t.addressFk  = vAdressFk AND t.routeFk =  vRouteFk;

END$$
DELIMITER ;
