DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`ticketRequest_beforeInsert`
	BEFORE INSERT ON `ticketRequest`
	FOR EACH ROW
BEGIN
	SET NEW.editorFk = account.myUser_getId();

	IF NEW.ticketFk IS NULL THEN
		SET NEW.ticketFk = (SELECT s.ticketFk FROM sale s WHERE s.id = NEW.saleFk);
	END IF;

	IF NEW.requesterFk IS NULL THEN
		SET NEW.requesterFk = (SELECT w.id FROM worker w WHERE w.code = NEW.salesPersonCode);
	END IF;

	IF NEW.attenderFk IS NULL THEN
		SET NEW.attenderFk = (SELECT w.id FROM worker w WHERE w.code = NEW.buyerCode);
	END IF;
END$$
DELIMITER ;
