2024-01-31 06:11:03 +00:00
|
|
|
DELIMITER $$
|
2024-08-20 08:06:10 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`collection_getAssigned`(
|
2024-01-31 06:11:03 +00:00
|
|
|
vUserFk INT,
|
2024-06-11 10:44:25 +00:00
|
|
|
OUT vCollectionFk INT
|
|
|
|
)
|
|
|
|
BEGIN
|
|
|
|
/**
|
2024-10-07 14:14:16 +00:00
|
|
|
* Comprueba si existen colecciones libres que se ajustan
|
|
|
|
* al perfil del usuario y le asigna la más antigua.
|
|
|
|
* Añade un registro al semillero de colecciones.
|
|
|
|
*
|
2024-01-31 06:11:03 +00:00
|
|
|
* @param vUserFk Id de usuario
|
|
|
|
* @param vCollectionFk Id de colección
|
|
|
|
*/
|
|
|
|
DECLARE vHasTooMuchCollections BOOL;
|
2024-10-07 14:14:16 +00:00
|
|
|
DECLARE vDone BOOL DEFAULT FALSE;
|
|
|
|
DECLARE vCollectionWorker INT;
|
|
|
|
DECLARE vMaxNotAssignedCollectionLifeTime TIME;
|
|
|
|
|
|
|
|
DECLARE vCollections CURSOR FOR
|
|
|
|
WITH collections AS (
|
|
|
|
SELECT tc.collectionFk,
|
|
|
|
SUM(sv.volume) volume,
|
|
|
|
c.saleTotalCount,
|
|
|
|
c.itemPackingTypeFk,
|
|
|
|
c.trainFk,
|
|
|
|
c.warehouseFk,
|
|
|
|
c.wagons
|
|
|
|
FROM vn.ticketCollection tc
|
|
|
|
JOIN vn.collection c ON c.id = tc.collectionFk
|
|
|
|
JOIN vn.saleVolume sv ON sv.ticketFk = tc.ticketFk
|
|
|
|
WHERE c.workerFk IS NULL
|
|
|
|
AND sv.shipped >= util.VN_CURDATE()
|
|
|
|
GROUP BY tc.collectionFk
|
|
|
|
) SELECT c.collectionFk
|
|
|
|
FROM collections c
|
|
|
|
JOIN vn.operator o
|
|
|
|
WHERE o.workerFk = vUserFk
|
|
|
|
AND (c.saleTotalCount <= o.linesLimit OR o.linesLimit IS NULL)
|
|
|
|
AND (c.itemPackingTypeFk = o.itemPackingTypeFk OR o.itemPackingTypeFk IS NULL)
|
|
|
|
AND o.numberOfWagons = c.wagons
|
|
|
|
AND o.trainFk = c.trainFk
|
|
|
|
AND o.warehouseFk = c.warehouseFk;
|
|
|
|
|
|
|
|
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
|
|
|
|
-- Si hay colecciones sin terminar, sale del proceso
|
2024-05-27 05:33:33 +00:00
|
|
|
|
|
|
|
DECLARE EXIT HANDLER FOR SQLEXCEPTION
|
|
|
|
BEGIN
|
2024-10-07 14:14:16 +00:00
|
|
|
ROLLBACK;
|
2024-05-27 05:33:33 +00:00
|
|
|
RESIGNAL;
|
|
|
|
END;
|
2024-01-31 06:11:03 +00:00
|
|
|
|
|
|
|
CALL collection_get(vUserFk);
|
|
|
|
|
2024-10-07 14:14:16 +00:00
|
|
|
SELECT (pc.maxNotReadyCollections - COUNT(*)) <= 0, pc.maxNotAssignedCollectionLifeTime
|
|
|
|
INTO vHasTooMuchCollections, vMaxNotAssignedCollectionLifeTime
|
|
|
|
FROM productionConfig pc
|
|
|
|
LEFT JOIN tmp.collection ON TRUE;
|
2024-01-31 06:11:03 +00:00
|
|
|
|
2024-07-04 07:32:56 +00:00
|
|
|
DROP TEMPORARY TABLE tmp.collection;
|
2024-01-31 06:11:03 +00:00
|
|
|
|
|
|
|
IF vHasTooMuchCollections THEN
|
2024-10-07 14:14:16 +00:00
|
|
|
CALL util.throw('Hay colecciones pendientes');
|
2024-01-31 06:11:03 +00:00
|
|
|
END IF;
|
|
|
|
|
|
|
|
-- Se eliminan las colecciones sin asignar que estan obsoletas
|
2024-10-07 14:14:16 +00:00
|
|
|
|
2024-01-31 06:11:03 +00:00
|
|
|
INSERT INTO ticketTracking(stateFk, ticketFk)
|
2024-10-07 14:14:16 +00:00
|
|
|
SELECT s.id, tc.ticketFk
|
|
|
|
FROM `collection` c
|
|
|
|
JOIN ticketCollection tc ON tc.collectionFk = c.id
|
|
|
|
JOIN `state` s ON s.code = 'PRINTED_AUTO'
|
2024-01-31 06:11:03 +00:00
|
|
|
WHERE c.workerFk IS NULL
|
2024-10-07 14:14:16 +00:00
|
|
|
AND TIMEDIFF(util.VN_NOW(), c.created) > vMaxNotAssignedCollectionLifeTime;
|
2024-01-31 06:11:03 +00:00
|
|
|
|
2024-10-07 14:14:16 +00:00
|
|
|
DELETE FROM `collection`
|
|
|
|
WHERE workerFk IS NULL
|
|
|
|
AND TIMEDIFF(util.VN_NOW(), created) > vMaxNotAssignedCollectionLifeTime;
|
2024-01-31 06:11:03 +00:00
|
|
|
|
|
|
|
-- Se añade registro al semillero
|
2024-10-07 14:14:16 +00:00
|
|
|
|
|
|
|
INSERT INTO collectionHotbed(userFk) VALUES(vUserFk);
|
2024-01-31 06:11:03 +00:00
|
|
|
|
|
|
|
-- Comprueba si hay colecciones disponibles que se ajustan a su configuracion
|
2024-10-07 14:14:16 +00:00
|
|
|
|
|
|
|
OPEN vCollections;
|
|
|
|
l: LOOP
|
|
|
|
SET vDone = FALSE;
|
|
|
|
FETCH vCollections INTO vCollectionFk;
|
|
|
|
|
|
|
|
IF vDone THEN
|
|
|
|
LEAVE l;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
DECLARE EXIT HANDLER FOR SQLEXCEPTION
|
|
|
|
BEGIN
|
|
|
|
ROLLBACK;
|
|
|
|
SET vCollectionFk = NULL;
|
|
|
|
RESIGNAL;
|
|
|
|
END;
|
|
|
|
|
|
|
|
START TRANSACTION;
|
|
|
|
|
|
|
|
SELECT workerFk INTO vCollectionWorker
|
|
|
|
FROM `collection`
|
|
|
|
WHERE id = vCollectionFk FOR UPDATE;
|
|
|
|
|
|
|
|
IF vCollectionWorker IS NULL THEN
|
|
|
|
UPDATE `collection`
|
|
|
|
SET workerFk = vUserFk
|
|
|
|
WHERE id = vCollectionFk;
|
|
|
|
|
|
|
|
COMMIT;
|
|
|
|
LEAVE l;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
ROLLBACK;
|
|
|
|
END;
|
|
|
|
END LOOP;
|
|
|
|
CLOSE vCollections;
|
2024-01-31 06:11:03 +00:00
|
|
|
|
|
|
|
IF vCollectionFk IS NULL THEN
|
|
|
|
CALL collection_new(vUserFk, vCollectionFk);
|
2024-10-07 14:14:16 +00:00
|
|
|
|
|
|
|
START TRANSACTION;
|
2024-01-31 06:11:03 +00:00
|
|
|
|
2024-10-07 14:14:16 +00:00
|
|
|
SELECT workerFk INTO vCollectionWorker
|
|
|
|
FROM `collection`
|
|
|
|
WHERE id = vCollectionFk FOR UPDATE;
|
2024-01-31 06:11:03 +00:00
|
|
|
|
2024-10-07 14:14:16 +00:00
|
|
|
IF vCollectionWorker IS NULL THEN
|
|
|
|
UPDATE `collection`
|
|
|
|
SET workerFk = vUserFk
|
|
|
|
WHERE id = vCollectionFk;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
COMMIT;
|
|
|
|
END IF;
|
2024-01-31 06:11:03 +00:00
|
|
|
CALL itemShelvingSale_addByCollection(vCollectionFk);
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|