2024-04-01 12:20:25 +00:00
|
|
|
DELIMITER $$
|
2024-01-25 16:33:54 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`collection_assign`(
|
|
|
|
vUserFk INT,
|
|
|
|
OUT vCollectionFk INT
|
2024-04-01 12:20:25 +00:00
|
|
|
)
|
2024-04-19 12:17:49 +00:00
|
|
|
BEGIN
|
2024-01-25 16:33:54 +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.
|
|
|
|
*
|
|
|
|
* @param vUserFk Id de usuario
|
|
|
|
* @param vCollectionFk Id de colección
|
|
|
|
*/
|
|
|
|
DECLARE vHasTooMuchCollections BOOL;
|
2024-06-25 12:00:46 +00:00
|
|
|
DECLARE vItemPackingTypeFk VARCHAR(1);
|
|
|
|
DECLARE vWarehouseFk INT;
|
|
|
|
DECLARE vLockName VARCHAR(215);
|
|
|
|
DECLARE vLockTime INT DEFAULT 30;
|
|
|
|
DECLARE vErrorNumber INT;
|
|
|
|
DECLARE vErrorMsg TEXT;
|
|
|
|
|
|
|
|
DECLARE EXIT HANDLER FOR SQLEXCEPTION
|
|
|
|
BEGIN
|
|
|
|
GET DIAGNOSTICS CONDITION 1
|
|
|
|
vErrorNumber = MYSQL_ERRNO,
|
|
|
|
vErrorMsg = MESSAGE_TEXT;
|
|
|
|
|
|
|
|
IF vLockName IS NOT NULL THEN
|
|
|
|
DO RELEASE_LOCK(vLockName);
|
|
|
|
CALL util.debugAdd('collection_assign', JSON_OBJECT(
|
|
|
|
'errorNumber', vErrorNumber,
|
|
|
|
'errorMsg', vErrorMsg,
|
|
|
|
'lockName', vLockName,
|
|
|
|
'userFk', vUserFk
|
|
|
|
)); -- Tmp
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
RESIGNAL;
|
|
|
|
END;
|
2024-04-01 12:20:25 +00:00
|
|
|
|
2024-01-25 16:33:54 +00:00
|
|
|
-- Si hay colecciones sin terminar, sale del proceso
|
|
|
|
CALL collection_get(vUserFk);
|
|
|
|
|
2024-06-25 12:00:46 +00:00
|
|
|
SELECT (pc.maxNotReadyCollections - COUNT(*)) <= 0,
|
|
|
|
collection_assign_lockname
|
|
|
|
INTO vHasTooMuchCollections,
|
|
|
|
vLockName
|
2024-04-29 12:54:15 +00:00
|
|
|
FROM productionConfig pc
|
2024-07-04 05:34:41 +00:00
|
|
|
LEFT JOIN tmp.collection ON TRUE;
|
2024-01-25 16:33:54 +00:00
|
|
|
|
2024-07-04 05:34:41 +00:00
|
|
|
DROP TEMPORARY TABLE tmp.collection;
|
2024-01-25 16:33:54 +00:00
|
|
|
|
|
|
|
IF vHasTooMuchCollections THEN
|
|
|
|
CALL util.throw('Hay colecciones pendientes');
|
|
|
|
END IF;
|
|
|
|
|
2024-06-25 12:00:46 +00:00
|
|
|
SELECT warehouseFk, itemPackingTypeFk
|
|
|
|
INTO vWarehouseFk, vItemPackingTypeFk
|
|
|
|
FROM operator
|
|
|
|
WHERE workerFk = vUserFk;
|
|
|
|
|
|
|
|
SET vLockName = CONCAT_WS('/',
|
|
|
|
vLockName,
|
|
|
|
vWarehouseFk,
|
|
|
|
vItemPackingTypeFk
|
|
|
|
);
|
|
|
|
|
|
|
|
IF NOT GET_LOCK(vLockName, vLockTime) THEN
|
|
|
|
CALL util.throw(CONCAT('Cannot get lock: ', vLockName));
|
|
|
|
END IF;
|
|
|
|
|
2024-01-25 16:33:54 +00:00
|
|
|
-- Se eliminan las colecciones sin asignar que estan obsoletas
|
|
|
|
INSERT INTO ticketTracking(stateFk, ticketFk)
|
|
|
|
SELECT s.id, tc.ticketFk
|
|
|
|
FROM `collection` c
|
|
|
|
JOIN ticketCollection tc ON tc.collectionFk = c.id
|
|
|
|
JOIN `state` s ON s.code = 'PRINTED_AUTO'
|
|
|
|
JOIN productionConfig pc
|
2024-06-25 12:00:46 +00:00
|
|
|
WHERE c.workerFk IS NULL
|
2024-01-25 16:33:54 +00:00
|
|
|
AND TIMEDIFF(util.VN_NOW(), c.created) > pc.maxNotAssignedCollectionLifeTime;
|
|
|
|
|
|
|
|
DELETE c.*
|
|
|
|
FROM `collection` c
|
|
|
|
JOIN productionConfig pc
|
|
|
|
WHERE c.workerFk IS NULL
|
|
|
|
AND TIMEDIFF(util.VN_NOW(), c.created) > pc.maxNotAssignedCollectionLifeTime;
|
|
|
|
|
|
|
|
-- Se añade registro al semillero
|
|
|
|
INSERT INTO collectionHotbed(userFk)
|
|
|
|
VALUES(vUserFk);
|
|
|
|
|
|
|
|
-- Comprueba si hay colecciones disponibles que se ajustan a su configuracion
|
2024-05-29 05:55:33 +00:00
|
|
|
SELECT MIN(c.id) INTO vCollectionFk
|
2024-01-25 16:33:54 +00:00
|
|
|
FROM `collection` c
|
|
|
|
JOIN operator o
|
|
|
|
ON (o.itemPackingTypeFk = c.itemPackingTypeFk OR c.itemPackingTypeFk IS NULL)
|
|
|
|
AND o.numberOfWagons = c.wagons
|
|
|
|
AND o.trainFk = c.trainFk
|
|
|
|
AND o.warehouseFk = c.warehouseFk
|
|
|
|
AND c.workerFk IS NULL
|
|
|
|
AND (c.saleTotalCount <= o.linesLimit OR o.linesLimit IS NULL)
|
|
|
|
JOIN (
|
|
|
|
SELECT tc.collectionFk, SUM(sv.volume) volume
|
|
|
|
FROM ticketCollection tc
|
|
|
|
JOIN saleVolume sv ON sv.ticketFk = tc.ticketFk
|
|
|
|
WHERE sv.shipped >= util.VN_CURDATE()
|
|
|
|
GROUP BY tc.collectionFk
|
|
|
|
) sub ON sub.collectionFk = c.id
|
|
|
|
AND (volume <= o.volumeLimit OR o.volumeLimit IS NULL)
|
|
|
|
WHERE o.workerFk = vUserFk;
|
|
|
|
|
|
|
|
IF vCollectionFk IS NULL THEN
|
|
|
|
CALL collection_new(vUserFk, vCollectionFk);
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
UPDATE `collection`
|
|
|
|
SET workerFk = vUserFk
|
|
|
|
WHERE id = vCollectionFk;
|
2024-06-25 12:00:46 +00:00
|
|
|
|
|
|
|
DO RELEASE_LOCK(vLockName);
|
2024-04-01 12:20:25 +00:00
|
|
|
END$$
|
|
|
|
DELIMITER ;
|