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

DELIMITER $$
CREATE DEFINER=`root`@`%` 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
 */
  	DECLARE vHoursFullTime  INT DEFAULT 40;
    
	DROP TEMPORARY TABLE IF EXISTS tmp.timeBusinessCalculate;
	DROP TEMPORARY TABLE IF EXISTS tmp.businessFullTime;
	
    CALL rangeDateInfo(vDatedFrom, vDatedTo);
    
	CREATE TEMPORARY TABLE tmp.timeBusinessCalculate
	SELECT dated, 
		    businessFk,
			userFk, 
			departmentFk, 
			hourStart,
			hourEnd,
            timeWorkSeconds,
            SEC_TO_TIME(timeWorkSeconds) timeWorkSexagesimal,
            timeWorkSeconds / 3600 timeWorkDecimal,
            timeWorkSeconds timeBusinessSeconds,
            SEC_TO_TIME(timeWorkSeconds) timeBusinessSexagesimal,
            timeWorkSeconds / 3600 timeBusinessDecimal,
			type,
            permissionrate,
			hoursWeek
		FROM(SELECT rd.dated,
					b.business_id businessFk,
                    w.userFk,
					bl.department_id departmentFk,  
					IF(cl.hours_week = vHoursFullTime, NULL, GROUP_CONCAT(DISTINCT LEFT(j.start,2) ORDER BY j.start ASC SEPARATOR '-')) hourStart , 
					IF(cl.hours_week = vHoursFullTime, NULL, GROUP_CONCAT(DISTINCT LEFT(j.end,2) ORDER BY j.end ASC SEPARATOR '-')) hourEnd,
					IF(cl.hours_week = vHoursFullTime, 0, IFNULL(SUM(TIME_TO_SEC(j.end)) - SUM(TIME_TO_SEC(j.start)),0)) timeWorkSeconds,
                    cs.type,
					cs.permissionRate,
					cl.hours_week hoursWeek
				FROM tmp.rangeDate rd
					LEFT JOIN postgresql.business b ON rd.dated BETWEEN b.date_start AND  ifnull(b.date_end, vDatedTo )
					LEFT JOIN postgresql.profile AS pr ON b.client_id = pr.profile_id
					LEFT JOIN postgresql.person AS p ON pr.person_id = p.person_id
					LEFT JOIN vn.worker AS w ON p.id_trabajador = w.id
                    JOIN tmp.`user` u ON u.userFK = w.userFK
					JOIN postgresql.business_labour AS bl ON b.business_id = bl.business_id
					LEFT JOIN postgresql.business_labour_payroll AS bp ON bl.business_id = bp.business_id
					LEFT JOIN postgresql.professional_category AS pc ON bl.professional_category_id = pc.professional_category_id
					LEFT JOIN postgresql.workcenter AS wc ON bl.workcenter_id = wc.workcenter_id
					LEFT JOIN postgresql.calendar_labour_type AS cl ON bl.calendar_labour_type_id = cl.calendar_labour_type_id
					LEFT JOIN postgresql.journey AS j ON j.business_id = b.business_id and j.day_id=WEEKDAY(rd.dated)+1
					LEFT JOIN postgresql.calendar_employee ce ON ce.business_id=b.business_id and  ce.date = rd.dated 
					LEFT JOIN postgresql.calendar_state cs ON cs.calendar_state_id = ce.calendar_state_id
				WHERE rd.dated BETWEEN vDatedFrom AND vDatedTo
				GROUP BY w.userFk,dated 
			)sub;
  
            
	UPDATE tmp.timeBusinessCalculate t
		SET t.timeWorkSeconds = vHoursFullTime / 5 * 3600,
			t.timeWorkSexagesimal = SEC_TO_TIME( vHoursFullTime / 5 * 3600), 
			t.timeWorkDecimal =  vHoursFullTime / 5, 
			t.timeBusinessSeconds = vHoursFullTime / 5 * 3600,
			t.timeBusinessSexagesimal = SEC_TO_TIME( vHoursFullTime / 5 * 3600), 
			t.timeBusinessDecimal = vHoursFullTime / 5
		WHERE DAYOFWEEK(t.dated) IN(2,3,4,5,6) AND hoursWeek = vHoursFullTime ;
  
    UPDATE tmp.timeBusinessCalculate t     
		SET t.timeWorkSeconds = t.timeWorkSeconds - (t.timeWorkSeconds * permissionrate)  ,
			t.timeWorkSexagesimal = SEC_TO_TIME(t.timeWorkSeconds - (t.timeWorkSeconds * permissionrate)), 
			t.timeWorkDecimal = t.timeWorkDecimal - (t.timeWorkDecimal * permissionrate)
		WHERE permissionrate <> 0;      
       
	UPDATE tmp.timeBusinessCalculate t     
		JOIN postgresql.calendar_labour cl ON cl.day = t.dated
		JOIN postgresql.business_labour bl ON bl.business_id = t.businessFk AND bl.workcenter_id = cl.workcenter_id 
		SET t.timeWorkSeconds = 0,
			t.timeWorkSexagesimal = 0, 
			t.timeWorkDecimal = 0,
            t.permissionrate = 1,
            t.type = 'Festivo'
		WHERE t.type IS NULL;

DROP TEMPORARY TABLE IF EXISTS tmp.rangeDate;
END$$

DELIMITER ;

