CREATE OR REPLACE DEFINER=`root`@`localhost`
	SQL SECURITY DEFINER
	VIEW `bs`.`bajasLaborales`
AS SELECT `w`.`firstName` AS `firstname`,
	`w`.`lastName` AS `name`,
	`b`.`id` AS `businessFk`,
	max(`c`.`dated`) AS `lastDate`,
	max(IFNULL(`b`.`ended`, `util`.`VN_CURDATE`())) AS `endContract`,
	`at`.`name` AS `type`,
	cast(COUNT(0) AS decimal(10, 0)) AS `dias`,
	`w`.`id` AS `userFk`
FROM (
		(
			(
				`vn`.`calendar` `c`
				JOIN `vn`.`business` `b` ON(`b`.`id` = `c`.`businessFk`)
			)
			JOIN `vn`.`worker` `w` ON(`w`.`id` = `b`.`workerFk`)
		)
		JOIN `vn`.`absenceType` `at` ON(`at`.`id` = `c`.`dayOffTypeFk`)
	)
WHERE `c`.`dated` >= `util`.`VN_CURDATE`() + INTERVAL -1 year
	AND `at`.`name` NOT IN (
		'Vacaciones',
		'Vacaciones 1/2 día',
		'Compensar',
		'Festivo'
	)
GROUP BY `w`.`id`,
	`at`.`id`
HAVING `endContract` >= `util`.`VN_CURDATE`()
