DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `srt`.`buffer_readPhotocell`(vSelf INT, vNumber INT)
BEGIN
/**
 * Establece el estado de un buffer en función del número de fotocélulas activas
 *
 * @param vSelf Identificador de srt.buffer
 * @param vNumber Número de fotocélulas activas
 */
	DECLARE vIsFull BOOL;
	DECLARE vBoxes INT;
	DECLARE vMotors INT;
	DECLARE vMargin INT;

	SELECT ((COUNT(*) + vNumber + c.motorsMargin) >= b.motors AND b.motors > 1), COUNT(*), b.motors, c.motorsMargin
		INTO vIsFull, vBoxes, vMotors, vMargin
		FROM srt.buffer b
			LEFT JOIN srt.moving m ON m.bufferToFk = b.id
			JOIN srt.config c
		WHERE b.id = vSelf;

	IF vIsFull THEN
		CALL srt.buffer_setStateType(vSelf, 'FULL', NULL);
	ELSE
		CALL srt.buffer_setStateType(vSelf, 'FREE', NULL);
	END IF;
END$$
DELIMITER ;