DROP PROCEDURE IF EXISTS `vn`.`timeBusiness_calculate`;

DELIMITER $$
$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `vn`.`timeBusiness_calculate`(vDatedFrom DATETIME, vDatedTo DATETIME)
BEGIN
/**
 * Horas que debe trabajar un empleado según contrato y día.
 * @param vDatedFrom workerTimeControl
 * @param vDatedTo workerTimeControl
 * @table tmp.user(userFk)
 * @return tmp.timeBusinessCalculate
 */
	DROP TEMPORARY TABLE IF EXISTS tmp.timeBusinessCalculate;
	CREATE TEMPORARY TABLE tmp.timeBusinessCalculate
			(INDEX (departmentFk))
		SELECT dated,
				businessFk,
				userFk,
				departmentFk,
				hourStart,
				hourEnd,
				timeTable,
				timeWorkSeconds,
				SEC_TO_TIME(timeWorkSeconds) timeWorkSexagesimal,
				timeWorkSeconds / 3600 timeWorkDecimal,
				timeWorkSeconds timeBusinessSeconds,
				SEC_TO_TIME(timeWorkSeconds) timeBusinessSexagesimal,
				timeWorkSeconds / 3600 timeBusinessDecimal,
				name type,
				permissionRate,
				hoursWeek,
				discountRate,
				isAllowedToWork
			FROM(SELECT t.dated,
						b.id businessFk,
						w.userFk,
						b.departmentFk,
						IF(j.start = NULL, NULL, GROUP_CONCAT(DISTINCT LEFT(j.start,5) ORDER BY j.start ASC SEPARATOR ' - ')) hourStart ,
						IF(j.start = NULL, NULL, GROUP_CONCAT(DISTINCT LEFT(j.end,5) ORDER BY j.end ASC SEPARATOR ' - ')) hourEnd,
						IF(j.start = NULL, NULL, GROUP_CONCAT(DISTINCT LEFT(j.start,5), " - ",  LEFT(j.end,5) ORDER BY j.end ASC SEPARATOR ' - ')) timeTable,
						IF(j.start = NULL, 0, IFNULL(SUM(TIME_TO_SEC(j.end)) - SUM(TIME_TO_SEC(j.start)), 0)) timeWorkSeconds,
						at2.name,
						at2.permissionRate,
						at2.discountRate,
						cl.hours_week hoursWeek,
						at2.isAllowedToWork
					FROM time t
						LEFT JOIN business b ON t.dated BETWEEN b.started AND IFNULL(b.ended, vDatedTo)
						LEFT JOIN worker w ON w.id = b.workerFk
						JOIN tmp.`user` u ON u.userFK = w.userFK
						LEFT JOIN workCenter wc ON wc.id = b.workcenterFK
						LEFT JOIN postgresql.calendar_labour_type cl ON cl.calendar_labour_type_id = b.calendarTypeFk
						LEFT JOIN postgresql.journey j ON j.business_id = b.id AND j.day_id = WEEKDAY(t.dated) + 1
						LEFT JOIN postgresql.calendar_employee ce ON ce.businessFk = b.id AND ce.date = t.dated
						LEFT JOIN absenceType at2 ON at2.id = ce.calendar_state_id
					WHERE t.dated BETWEEN vDatedFrom AND vDatedTo
					GROUP BY w.userFk, t.dated
				)sub;

	UPDATE tmp.timeBusinessCalculate t
		LEFT JOIN  postgresql.journey j ON j.business_id = t.businessFk
		SET t.timeWorkSeconds = t.hoursWeek / 5 * 3600,
			t.timeWorkSexagesimal = SEC_TO_TIME( t.hoursWeek / 5 * 3600),
			t.timeWorkDecimal =  t.hoursWeek / 5,
			t.timeBusinessSeconds = t.hoursWeek / 5 * 3600,
			t.timeBusinessSexagesimal = SEC_TO_TIME( t.hoursWeek / 5 * 3600),
			t.timeBusinessDecimal = t.hoursWeek / 5
		WHERE DAYOFWEEK(t.dated) IN(2,3,4,5,6) AND j.journey_id IS NULL  ;

	UPDATE tmp.timeBusinessCalculate t
		SET t.timeWorkSeconds = t.timeWorkSeconds - (t.timeWorkSeconds * permissionRate)  ,
			t.timeWorkSexagesimal = SEC_TO_TIME ((t.timeWorkDecimal - (t.timeWorkDecimal * permissionRate)) * 3600),
			t.timeWorkDecimal = t.timeWorkDecimal - (t.timeWorkDecimal * permissionRate)
		WHERE permissionRate <> 0;

	UPDATE tmp.timeBusinessCalculate t
			JOIN calendarHolidays ch ON ch.dated = t.dated
			JOIN business b ON b.id = t.businessFk
				AND b.workcenterFk = ch.workcenterFk
		SET t.timeWorkSeconds = 0,
			t.timeWorkSexagesimal = 0,
			t.timeWorkDecimal = 0,
			t.permissionrate = 1,
			t.type = 'Festivo'
		WHERE t.type IS NULL;
END$$
DELIMITER ;
