2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-07-18 13:14:18 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`expeditionPallet_build`(
|
|
|
|
vExpeditions JSON,
|
|
|
|
vArcId INT,
|
|
|
|
vWorkerFk INT,
|
|
|
|
OUT vPalletFk INT
|
|
|
|
)
|
2024-01-15 11:31:03 +00:00
|
|
|
BEGIN
|
|
|
|
/** Construye un pallet de expediciones.
|
|
|
|
*
|
|
|
|
* Primero comprueba si esas expediciones ya pertenecen a otro pallet,
|
|
|
|
* en cuyo caso actualiza ese pallet.
|
|
|
|
*
|
|
|
|
* @param vExpeditions JSON_ARRAY con esta estructura [exp1, exp2, exp3, ...]
|
2024-07-18 13:14:18 +00:00
|
|
|
* @param vArcId INT Identificador de arcRead
|
|
|
|
* @param vWorkerFk INT Identificador de worker
|
|
|
|
* @param out vPalletFk Identificador de expeditionPallet
|
2024-01-15 11:31:03 +00:00
|
|
|
*/
|
|
|
|
DECLARE vCounter INT;
|
|
|
|
DECLARE vExpeditionFk INT;
|
|
|
|
DECLARE vTruckFk INT;
|
|
|
|
DECLARE vPrinterFk INT;
|
2024-07-22 07:46:20 +00:00
|
|
|
DECLARE vExpeditionStateTypeFk INT;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-07-18 13:14:18 +00:00
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tExpedition (
|
|
|
|
expeditionFk INT,
|
|
|
|
routeFk INT,
|
|
|
|
palletFk INT,
|
|
|
|
PRIMARY KEY (expeditionFk)
|
|
|
|
);
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
SET vCounter = JSON_LENGTH(vExpeditions);
|
|
|
|
|
|
|
|
WHILE vCounter DO
|
|
|
|
SET vCounter = vCounter - 1;
|
|
|
|
|
|
|
|
SET vExpeditionFk = JSON_EXTRACT(vExpeditions,CONCAT("$[", vCounter, "]"));
|
|
|
|
|
|
|
|
INSERT IGNORE INTO tExpedition(expeditionFk, routeFk, palletFk)
|
|
|
|
SELECT vExpeditionFk, t.routeFk, es.palletFk
|
2024-07-18 13:14:18 +00:00
|
|
|
FROM expedition e
|
|
|
|
LEFT JOIN ticket t ON t.id = e.ticketFk
|
|
|
|
LEFT JOIN expeditionScan es ON es.expeditionFk = e.id
|
2024-01-15 11:31:03 +00:00
|
|
|
WHERE e.id = vExpeditionFk;
|
|
|
|
END WHILE;
|
|
|
|
|
|
|
|
SELECT palletFk INTO vPalletFk
|
|
|
|
FROM (
|
2024-07-18 13:14:18 +00:00
|
|
|
SELECT palletFk, count(*) n
|
|
|
|
FROM tExpedition
|
|
|
|
WHERE palletFk > 0
|
|
|
|
GROUP BY palletFk
|
|
|
|
ORDER BY n DESC
|
|
|
|
LIMIT 100
|
|
|
|
) sub
|
2024-01-15 11:31:03 +00:00
|
|
|
LIMIT 1;
|
|
|
|
|
|
|
|
IF vPalletFk IS NULL THEN
|
2024-07-18 13:14:18 +00:00
|
|
|
SELECT roadmapStopFk INTO vTruckFk
|
|
|
|
FROM (
|
|
|
|
SELECT rm.roadmapStopFk, count(*) n
|
|
|
|
FROM routesMonitor rm
|
|
|
|
JOIN tExpedition e ON e.routeFk = rm.routeFk
|
|
|
|
GROUP BY roadmapStopFk
|
|
|
|
ORDER BY n DESC
|
|
|
|
LIMIT 1
|
|
|
|
) sub;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
IF vTruckFk IS NULL THEN
|
|
|
|
CALL util.throw ('TRUCK_NOT_AVAILABLE');
|
|
|
|
END IF;
|
|
|
|
|
2024-07-18 13:16:25 +00:00
|
|
|
INSERT INTO expeditionPallet SET truckFk = vTruckFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
SET vPalletFk = LAST_INSERT_ID();
|
|
|
|
END IF;
|
|
|
|
|
2024-07-18 13:14:18 +00:00
|
|
|
INSERT INTO expeditionScan(expeditionFk, palletFk, workerFk)
|
2024-01-15 11:31:03 +00:00
|
|
|
SELECT expeditionFk, vPalletFk, vWorkerFk
|
|
|
|
FROM tExpedition
|
|
|
|
ON DUPLICATE KEY UPDATE palletFk = vPalletFk, workerFk = vWorkerFk;
|
|
|
|
|
2024-07-22 07:49:54 +00:00
|
|
|
SELECT id INTO vExpeditionStateTypeFk
|
|
|
|
FROM expeditionStateType
|
|
|
|
WHERE code = 'PALLETIZED';
|
2024-07-18 13:14:18 +00:00
|
|
|
|
|
|
|
INSERT INTO expeditionState(expeditionFk, typeFk)
|
2024-07-22 07:46:20 +00:00
|
|
|
SELECT expeditionFk, vExpeditionStateTypeFk FROM tExpedition;
|
2024-07-18 13:14:18 +00:00
|
|
|
|
|
|
|
SELECT printerFk INTO vPrinterFk FROM arcRead WHERE id = vArcId;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-07-18 13:14:18 +00:00
|
|
|
CALL report_print(
|
2024-01-15 11:31:03 +00:00
|
|
|
'LabelPalletExpedition',
|
|
|
|
vPrinterFk,
|
|
|
|
account.myUser_getId(),
|
|
|
|
JSON_OBJECT('palletFk', vPalletFk, 'userFk', account.myUser_getId()),
|
|
|
|
'high'
|
|
|
|
);
|
|
|
|
|
2024-07-18 13:14:18 +00:00
|
|
|
UPDATE expeditionPallet SET isPrint = TRUE WHERE id = vPalletFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
DROP TEMPORARY TABLE tExpedition;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|