This commit is contained in:
parent
cd7ed6987a
commit
2d3ae5ce9e
|
@ -1,13 +1,13 @@
|
||||||
DELIMITER $$
|
DELIMITER $$
|
||||||
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`workerTimeControl_clockIn`(
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`workerTimeControl_clockIn`(
|
||||||
vWorkerFk INT,
|
vWorkerFk INT,
|
||||||
vTimed DATETIME,
|
vTimed DATETIME,
|
||||||
vDirection VARCHAR(10),
|
vDirection VARCHAR(10),
|
||||||
vDevice VARCHAR(255)
|
vDevice VARCHAR(255)
|
||||||
)
|
)
|
||||||
BEGIN
|
BEGIN
|
||||||
/**
|
/**
|
||||||
* Verifica si el empleado puede fichar
|
* Verifica si el empleado puede fichar, en caso de que sí ficha.
|
||||||
* @param vWorkerFk Identificador del trabajador
|
* @param vWorkerFk Identificador del trabajador
|
||||||
* @param vTimed Balor de la fichada, IF vTimed IS NULL vTimed = NOW
|
* @param vTimed Balor de la fichada, IF vTimed IS NULL vTimed = NOW
|
||||||
* @param vDirection Solo se pueden pasa los valores del campo
|
* @param vDirection Solo se pueden pasa los valores del campo
|
||||||
|
@ -18,7 +18,6 @@ BEGIN
|
||||||
* Solo retorna el primer problema, en caso de no ocurrir ningún error se añadirá
|
* Solo retorna el primer problema, en caso de no ocurrir ningún error se añadirá
|
||||||
* fichada a la tabla vn.workerTimeControl
|
* fichada a la tabla vn.workerTimeControl
|
||||||
*/
|
*/
|
||||||
|
|
||||||
DECLARE vLastIn DATETIME;
|
DECLARE vLastIn DATETIME;
|
||||||
DECLARE vLastOut DATETIME;
|
DECLARE vLastOut DATETIME;
|
||||||
DECLARE vNextIn DATETIME;
|
DECLARE vNextIn DATETIME;
|
||||||
|
@ -40,6 +39,7 @@ BEGIN
|
||||||
DECLARE vIsManual BOOLEAN DEFAULT TRUE;
|
DECLARE vIsManual BOOLEAN DEFAULT TRUE;
|
||||||
DECLARE vMaxWorkShortCycle INT;
|
DECLARE vMaxWorkShortCycle INT;
|
||||||
DECLARE vMaxWorkLongCycle INT;
|
DECLARE vMaxWorkLongCycle INT;
|
||||||
|
DECLARE vIsCampaing BOOL;
|
||||||
|
|
||||||
DECLARE EXIT HANDLER FOR SQLSTATE '45000'
|
DECLARE EXIT HANDLER FOR SQLSTATE '45000'
|
||||||
BEGIN
|
BEGIN
|
||||||
|
@ -75,11 +75,16 @@ BEGIN
|
||||||
|
|
||||||
SET vDated = DATE(vTimed);
|
SET vDated = DATE(vTimed);
|
||||||
|
|
||||||
|
SELECT EXISTS (
|
||||||
|
SELECT TRUE FROM campaign
|
||||||
|
WHERE vTimed BETWEEN dated - INTERVAL scopeDays DAY AND dated
|
||||||
|
) INTO vIsCampaing;
|
||||||
|
|
||||||
SELECT IF(pc.code = 'driverCE',
|
SELECT IF(pc.code = 'driverCE',
|
||||||
wc.dayBreakDriver,
|
wc.dayBreakDriver,
|
||||||
wc.dayBreak),
|
wc.dayBreak),
|
||||||
wc.shortWeekBreak,
|
wc.shortWeekBreak,
|
||||||
wc.longWeekBreak,
|
IF(vIsCampaing, NULL, wc.longWeekBreak),
|
||||||
wc.weekScope,
|
wc.weekScope,
|
||||||
wc.dayMaxTime,
|
wc.dayMaxTime,
|
||||||
wc.maxWorkShortCycle,
|
wc.maxWorkShortCycle,
|
||||||
|
@ -97,19 +102,19 @@ BEGIN
|
||||||
JOIN workerTimeControlConfig wc
|
JOIN workerTimeControlConfig wc
|
||||||
WHERE b.workerFk = vWorkerFk
|
WHERE b.workerFk = vWorkerFk
|
||||||
AND vDated BETWEEN b.started AND IFNULL(b.ended, vDated);
|
AND vDated BETWEEN b.started AND IFNULL(b.ended, vDated);
|
||||||
|
|
||||||
-- CONTRATO EN VIGOR
|
-- CONTRATO EN VIGOR
|
||||||
IF vDayBreak IS NULL THEN
|
IF vDayBreak IS NULL THEN
|
||||||
SET vErrorCode = 'INACTIVE_BUSINESS';
|
SET vErrorCode = 'INACTIVE_BUSINESS';
|
||||||
CALL util.throw(vErrorCode);
|
CALL util.throw(vErrorCode);
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
-- FICHADAS A FUTURO
|
-- FICHADAS A FUTURO
|
||||||
IF vTimed > util.VN_NOW() + INTERVAL 1 MINUTE THEN
|
IF vTimed > util.VN_NOW() + INTERVAL 1 MINUTE THEN
|
||||||
SET vErrorCode = 'IS_NOT_ALLOWED_FUTURE';
|
SET vErrorCode = 'IS_NOT_ALLOWED_FUTURE';
|
||||||
CALL util.throw(vErrorCode);
|
CALL util.throw(vErrorCode);
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
-- VERIFICAR SI ESTÁ PERMITIDO TRABAJAR
|
-- VERIFICAR SI ESTÁ PERMITIDO TRABAJAR
|
||||||
CALL timeBusiness_calculateByWorker(vWorkerFk, vDated, vDated);
|
CALL timeBusiness_calculateByWorker(vWorkerFk, vDated, vDated);
|
||||||
SELECT isAllowedToWork INTO vIsAllowedToWork
|
SELECT isAllowedToWork INTO vIsAllowedToWork
|
||||||
|
@ -121,7 +126,6 @@ BEGIN
|
||||||
CALL util.throw(vErrorCode);
|
CALL util.throw(vErrorCode);
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
|
|
||||||
-- DIRECCION CORRECTA
|
-- DIRECCION CORRECTA
|
||||||
CALL workerTimeControl_direction(vWorkerFk, vTimed);
|
CALL workerTimeControl_direction(vWorkerFk, vTimed);
|
||||||
IF (SELECT
|
IF (SELECT
|
||||||
|
@ -138,8 +142,7 @@ BEGIN
|
||||||
) THEN
|
) THEN
|
||||||
SET vIsError = TRUE;
|
SET vIsError = TRUE;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
|
|
||||||
IF vIsError THEN
|
IF vIsError THEN
|
||||||
SET vErrorCode = 'WRONG_DIRECTION';
|
SET vErrorCode = 'WRONG_DIRECTION';
|
||||||
IF(SELECT option1 IS NULL AND option2 IS NULL
|
IF(SELECT option1 IS NULL AND option2 IS NULL
|
||||||
|
@ -150,6 +153,7 @@ BEGIN
|
||||||
CALL util.throw(vErrorCode);
|
CALL util.throw(vErrorCode);
|
||||||
END IF;
|
END IF;
|
||||||
DROP TEMPORARY TABLE tmp.workerTimeControlDirection;
|
DROP TEMPORARY TABLE tmp.workerTimeControlDirection;
|
||||||
|
|
||||||
-- FICHADAS IMPARES
|
-- FICHADAS IMPARES
|
||||||
SELECT timed INTO vLastIn
|
SELECT timed INTO vLastIn
|
||||||
FROM workerTimeControl
|
FROM workerTimeControl
|
||||||
|
@ -158,7 +162,7 @@ BEGIN
|
||||||
AND timed < vTimed
|
AND timed < vTimed
|
||||||
ORDER BY timed DESC
|
ORDER BY timed DESC
|
||||||
LIMIT 1;
|
LIMIT 1;
|
||||||
|
|
||||||
IF (SELECT IF(vDirection = 'in',
|
IF (SELECT IF(vDirection = 'in',
|
||||||
MOD(COUNT(*), 2) ,
|
MOD(COUNT(*), 2) ,
|
||||||
IF (vDirection = 'out', NOT MOD(COUNT(*), 2), FALSE))
|
IF (vDirection = 'out', NOT MOD(COUNT(*), 2), FALSE))
|
||||||
|
@ -169,7 +173,7 @@ BEGIN
|
||||||
SET vErrorCode = 'ODD_WORKERTIMECONTROL';
|
SET vErrorCode = 'ODD_WORKERTIMECONTROL';
|
||||||
CALL util.throw(vErrorCode);
|
CALL util.throw(vErrorCode);
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
-- DESCANSO DIARIO
|
-- DESCANSO DIARIO
|
||||||
SELECT timed INTO vLastOut
|
SELECT timed INTO vLastOut
|
||||||
FROM workerTimeControl
|
FROM workerTimeControl
|
||||||
|
@ -178,7 +182,7 @@ BEGIN
|
||||||
AND timed < vTimed
|
AND timed < vTimed
|
||||||
ORDER BY timed DESC
|
ORDER BY timed DESC
|
||||||
LIMIT 1;
|
LIMIT 1;
|
||||||
|
|
||||||
SELECT timed INTO vNextIn
|
SELECT timed INTO vNextIn
|
||||||
FROM workerTimeControl
|
FROM workerTimeControl
|
||||||
WHERE userFk = vWorkerFk
|
WHERE userFk = vWorkerFk
|
||||||
|
@ -186,7 +190,7 @@ BEGIN
|
||||||
AND timed > vTimed
|
AND timed > vTimed
|
||||||
ORDER BY timed ASC
|
ORDER BY timed ASC
|
||||||
LIMIT 1;
|
LIMIT 1;
|
||||||
|
|
||||||
CASE vDirection
|
CASE vDirection
|
||||||
WHEN 'in' THEN
|
WHEN 'in' THEN
|
||||||
IF UNIX_TIMESTAMP(vTimed) - UNIX_TIMESTAMP(vLastOut) <= vDayBreak THEN
|
IF UNIX_TIMESTAMP(vTimed) - UNIX_TIMESTAMP(vLastOut) <= vDayBreak THEN
|
||||||
|
@ -204,11 +208,8 @@ BEGIN
|
||||||
CALL util.throw(vErrorCode);
|
CALL util.throw(vErrorCode);
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
IF (vDirection IN('in', 'out')) THEN
|
IF (vDirection IN('in', 'out')) THEN
|
||||||
-- VERIFICA MAXIMO TIEMPO DESDE ENTRADA HASTA LA SALIDA
|
-- VERIFICA MAXIMO TIEMPO DESDE ENTRADA HASTA LA SALIDA
|
||||||
|
|
||||||
SELECT timed INTO vNextOut
|
SELECT timed INTO vNextOut
|
||||||
FROM workerTimeControl
|
FROM workerTimeControl
|
||||||
WHERE userFk = vWorkerFk
|
WHERE userFk = vWorkerFk
|
||||||
|
@ -216,7 +217,7 @@ BEGIN
|
||||||
AND timed > vTimed
|
AND timed > vTimed
|
||||||
ORDER BY timed ASC
|
ORDER BY timed ASC
|
||||||
LIMIT 1;
|
LIMIT 1;
|
||||||
|
|
||||||
SELECT direction INTO vNextDirection
|
SELECT direction INTO vNextDirection
|
||||||
FROM workerTimeControl
|
FROM workerTimeControl
|
||||||
WHERE userFk = vWorkerFk
|
WHERE userFk = vWorkerFk
|
||||||
|
@ -224,7 +225,7 @@ BEGIN
|
||||||
AND timed > vTimed
|
AND timed > vTimed
|
||||||
ORDER BY timed ASC
|
ORDER BY timed ASC
|
||||||
LIMIT 1;
|
LIMIT 1;
|
||||||
|
|
||||||
SELECT direction INTO vLastDirection
|
SELECT direction INTO vLastDirection
|
||||||
FROM workerTimeControl
|
FROM workerTimeControl
|
||||||
WHERE userFk = vWorkerFk
|
WHERE userFk = vWorkerFk
|
||||||
|
@ -232,7 +233,7 @@ BEGIN
|
||||||
AND timed < vTimed
|
AND timed < vTimed
|
||||||
ORDER BY timed ASC
|
ORDER BY timed ASC
|
||||||
LIMIT 1;
|
LIMIT 1;
|
||||||
|
|
||||||
IF (vDirection ='in'
|
IF (vDirection ='in'
|
||||||
AND vNextDirection = 'out'
|
AND vNextDirection = 'out'
|
||||||
AND UNIX_TIMESTAMP(vNextOut) - UNIX_TIMESTAMP(vTimed) > vDayMaxTime) OR
|
AND UNIX_TIMESTAMP(vNextOut) - UNIX_TIMESTAMP(vTimed) > vDayMaxTime) OR
|
||||||
|
@ -242,9 +243,8 @@ BEGIN
|
||||||
SET vErrorCode = 'DAY_MAX_TIME';
|
SET vErrorCode = 'DAY_MAX_TIME';
|
||||||
CALL util.throw(vErrorCode);
|
CALL util.throw(vErrorCode);
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
-- VERIFICA DESCANSO SEMANAL
|
-- VERIFICA DESCANSO SEMANAL
|
||||||
|
|
||||||
WITH wtc AS(
|
WITH wtc AS(
|
||||||
(SELECT timed
|
(SELECT timed
|
||||||
FROM vn.workerTimeControl
|
FROM vn.workerTimeControl
|
||||||
|
@ -271,7 +271,7 @@ BEGIN
|
||||||
LEAD(hasLongBreak) OVER (ORDER BY timed) nextHasLongBreak
|
LEAD(hasLongBreak) OVER (ORDER BY timed) nextHasLongBreak
|
||||||
FROM wtcBreak
|
FROM wtcBreak
|
||||||
)SELECT TIMESTAMPDIFF(SECOND, MIN(timed), MAX(timed)) > vMaxWorkLongCycle OR
|
)SELECT TIMESTAMPDIFF(SECOND, MIN(timed), MAX(timed)) > vMaxWorkLongCycle OR
|
||||||
(TIMESTAMPDIFF(SECOND, MIN(timed), MAX(timed))> vMaxWorkShortCycle
|
(TIMESTAMPDIFF(SECOND, MIN(timed), MAX(timed)) > vMaxWorkShortCycle
|
||||||
AND NOT SUM(IFNULL(nextHasLongBreak, 1)))
|
AND NOT SUM(IFNULL(nextHasLongBreak, 1)))
|
||||||
hasError INTO vIsError
|
hasError INTO vIsError
|
||||||
FROM wtcBreakCounter
|
FROM wtcBreakCounter
|
||||||
|
@ -290,6 +290,5 @@ BEGIN
|
||||||
VALUES(vWorkerFk, vTimed, vDirection, vDevice, vIsManual);
|
VALUES(vWorkerFk, vTimed, vDirection, vDevice, vIsManual);
|
||||||
|
|
||||||
SELECT LAST_INSERT_ID() id;
|
SELECT LAST_INSERT_ID() id;
|
||||||
|
|
||||||
END$$
|
END$$
|
||||||
DELIMITER ;
|
DELIMITER ;
|
||||||
|
|
Loading…
Reference in New Issue