DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`sectorCollection_get`()
BEGIN
/**
 * Devuelve las colecciones del sector que corresponden al usuario conectado, y que estan incompletas
 *
 */
	SELECT DISTINCT sc.id collectionFk, sc.created
		FROM vn.sectorCollection sc
			JOIN vn.state s ON s.code = 'PREVIOUS_PREPARATION'
			LEFT JOIN vn.sectorCollectionSaleGroup scsg ON scsg.sectorCollectionFk = sc.id
			LEFT JOIN vn.saleGroupDetail sgd ON sgd.saleGroupFk = scsg.saleGroupFk
			LEFT JOIN vn.saleTracking st ON st.saleFk = sgd.saleFk AND st.stateFk = s.id AND st.workerFk = sc.userFk
			LEFT JOIN vn.sale sl ON sl.id = st.saleFk
		WHERE sc.userFk = account.myUser_getId()
			AND (
				ISNULL(scsg.sectorCollectionFk)
				OR (NOT st.isChecked AND sl.quantity > 0)
			)
			AND sc.created > util.VN_CURDATE();
END$$
DELIMITER ;
