DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`workerCreateExternal`(
	vFirstName VARCHAR(50),
	vSurname1 VARCHAR(50),
	vSurname2 VARCHAR(50),
	vUser VARCHAR(20),
	vPassword VARCHAR(50),
	vWorkerCode VARCHAR(3),
	vRole INT(2)
	)
BEGIN
	DECLARE vUserId INT;
	DECLARE vSurnames VARCHAR(100);

	INSERT INTO account.`user`(name, nickname, password, `role`, hasGrant)
		SELECT vUser, vUser, MD5(vPassword), vRole, false;

	SET vUserId = LAST_INSERT_ID();

	IF vSurname2 IS NULL THEN
		SET vSurnames = vSurname1;
	ELSE
		SET vSurnames = CONCAT(vSurname1, ' ', vSurname2);
	END IF;

	INSERT INTO worker(id, firstName, lastName, code, isFreelance)
		SELECT vUserId, vFirstName, vSurnames, vWorkerCode, true;
END$$
DELIMITER ;
