
USE `vn`;
DROP procedure IF EXISTS `vn`.`stowawayUnBoarding`;

DELIMITER $$
USE `vn`$$
CREATE DEFINER=`root`@`%` PROCEDURE `stowaway_unboarding`(vShipFk INT, vStowawayFk INT)
BEGIN
    DECLARE vWorker VARCHAR(255);
    
	DELETE FROM stowaway 
		WHERE shipFk = vShipFk AND id = vStowawayFk;
    
    DELETE tt FROM ticketTracking tt
		JOIN state s ON s.id = tt.stateFk
            WHERE code = 'BOARDING' AND ticketFk = vShipFk; 
   	
    DELETE FROM sale 
		WHERE ticketFk = vShipFk 
			AND itemFk = 98 
            AND concept = CONCAT('POLIZÓN! ',vStowawayFk);         
        
	SELECT u.`name` INTO vWorker 
		FROM account.user u JOIN worker w ON w.userFk = u.id
		WHERE w.id = client_getSalesPersonByTicket(vStowawayFk);
            
	SELECT messageSend(vWorker,CONCAT('El ticket: ', vStowawayFk, ' ha dejado de ser un polizón')) INTO @a;
END$$

DELIMITER ;
;
