ALTER TABLE `vn`.`operator` ADD sectorFk int(11) NULL;
ALTER TABLE `vn`.`operator` ADD labelerFk tinyint(3) unsigned NULL;
ALTER TABLE `vn`.`operator` ADD CONSTRAINT operator_FK_5 FOREIGN KEY (labelerFk) REFERENCES `vn`.`printer`(id) ON DELETE CASCADE ON UPDATE CASCADE;

UPDATE `vn`.`operator` o 
JOIN (SELECT id, sectorFk, labelerFk
			FROM `vn`.`worker`) sub ON sub.id = o.workerFk
	SET o.sectorFk = sub.sectorFk,
		o.labelerFk = sub.labelerFk;

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`collection_printSticker`(
	vSelf INT, 
	vLabelCount INT
)
BEGIN
/**
 * Prints a yellow label from a collection or a ticket 
 *
 * @param vSelf collection or ticket
 * @param vLabelCount number of times the collection has been printed
 */
	DECLARE vPrintArgs JSON DEFAULT JSON_OBJECT('collectionOrTicketFk', vSelf);

	IF vLabelCount IS NULL THEN 	
		INSERT INTO ticketTrolley
			SELECT ticketFk, 1
				FROM ticketCollection 
				WHERE collectionFk = vSelf
					ON DUPLICATE KEY UPDATE labelCount = labelCount + 1;
	ELSE
		SET vPrintArgs = JSON_MERGE_PATCH(vPrintArgs, JSON_OBJECT('labelCount', vLabelCount));
	END IF;

	CALL report_print(
			'LabelCollection', 
			(SELECT o.labelerFk FROM operator o WHERE o.workerFk = account.myUser_getId()),
			account.myUser_getId(), 
			vPrintArgs,
			'high'
		);
END$$
DELIMITER ;

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`expeditionPallet_printLabel`(vSelf INT)
BEGIN
/**
 * Calls the report_print procedure and passes it 
 * the necessary parameters for printing.
 *
 * @param vSelf expeditioPallet id.
 */
	DECLARE vPrinterFk INT;
	DECLARE vUserFk INT DEFAULT account.myUser_getId();
	
	SELECT o.labelerFk INTO vPrinterFk
		FROM operator o
		WHERE o.workerFk = vUserFk;

	CALL vn.report_print(
		'LabelPalletExpedition',
		vPrinterFk,
		account.myUser_getId(),
		JSON_OBJECT('palletFk', vSelf, 'userFk', vUserFk),
		'high'
	);

	UPDATE vn.expeditionPallet
		SET isPrint = TRUE
		WHERE id = vSelf;
END$$
DELIMITER ;

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`itemShelving_getAlternatives`(vShelvingFk VARCHAR(10))
BEGIN
/**
 * Devuelve un listado de posibles ubicaciones alternativas a ubicar los item de la matricula
 * del carro que se le ha pasado.
 *
 * @param vShelvingFk matricula del carro
 */
	SELECT is2.id,is2.shelvingFk , p.code, is2.itemFk , is2.visible, p.pickingOrder 
		FROM itemShelving is2
			JOIN shelving sh ON sh.code = is2.shelvingFk
			JOIN parking p ON p.id = sh.parkingFk
			JOIN sector s ON s.id = p.sectorFk
			LEFT JOIN operator o ON  o.sectorFk = s.id
			LEFT JOIN worker w ON w.sectorFk = s.id  AND w.id = account.myUser_getId()
			JOIN warehouse wh ON wh.id = s.warehouseFk
			JOIN itemShelving is3 ON is3.itemFk = is2.itemFk AND is3.shelvingFk  = vShelvingFk COLLATE utf8_unicode_ci
		WHERE is2.shelvingFk <> vShelvingFk COLLATE utf8_unicode_ci
		GROUP BY is2.id
		ORDER BY p.pickingOrder DESC;
END$$
DELIMITER ;

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`operator_beforeInsert`
	BEFORE INSERT ON `operator`
	FOR EACH ROW
BEGIN
	CALL vn.printer_checkSector(NEW.labelerFk, NEW.sectorFk);
END$$
DELIMITER ;

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`operator_beforeUpdate`
	BEFORE UPDATE ON `operator`
	FOR EACH ROW
BEGIN
	IF NOT (NEW.labelerFk <=> OLD.labelerFk AND NEW.sectorFk <=> OLD.sectorFk) THEN	
		CALL vn.printer_checkSector(NEW.labelerFk, NEW.sectorFk);
	END IF;
END$$
DELIMITER ;

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`worker_beforeUpdate`
	BEFORE UPDATE ON `worker`
	FOR EACH ROW
BEGIN
	IF NOT (NEW.labelerFk <=> OLD.labelerFk AND NEW.sectorFk <=> OLD.sectorFk) THEN	
		CALL vn.printer_checkSector(NEW.labelerFk, NEW.sectorFk);

		INSERT IGNORE INTO vn.operator (workerFk)
			VALUES (NEW.id);

		UPDATE operator 
			SET labelerFk = NEW.labelerFk,
				sectorFk = NEW.sectorFk
			WHERE workerFk = NEW.id;
	END IF;
END$$
DELIMITER ;

CREATE OR REPLACE DEFINER=`root`@`localhost`
	SQL SECURITY DEFINER
	VIEW `vn`.`operatorWorkerCode`
AS SELECT `o`.`workerFk` AS `workerFk`,
	concat(`w`.`firstName`, ' ', `w`.`lastName`) AS `fullName`,
	`w`.`code` AS `code`,
	`o`.`numberOfWagons` AS `numberOfWagons`
FROM (
		(
			`vn`.`worker` `w`
			JOIN `vn`.`operator` `o` ON(`o`.`workerFk` = `w`.`id`)
		)
		JOIN `vn`.`sector` `s` ON(`o`.`sectorFk` = `s`.`id`)
	)
WHERE `o`.`sectorFk` IS NOT NULL
	AND `s`.`code` IN (
		'H2',
		'H2',
		'PEQUES_H',
		'ALTILLO COMP',
		'ALTILLO ARTI'
	)