2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-08-20 08:06:10 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`expeditionPallet_build`(
|
2024-07-18 13:14:18 +00:00
|
|
|
vExpeditions JSON,
|
|
|
|
vArcId INT,
|
|
|
|
vWorkerFk INT,
|
|
|
|
OUT vPalletFk INT
|
|
|
|
)
|
2024-10-04 05:52:13 +00:00
|
|
|
proc: BEGIN
|
2024-08-08 10:46:10 +00:00
|
|
|
/**
|
|
|
|
* Builds an expedition pallet.
|
2024-01-15 11:31:03 +00:00
|
|
|
*
|
2024-08-08 10:46:10 +00:00
|
|
|
* First, it checks if these expeditions already belong to another pallet,
|
|
|
|
* in which case it returns an error.
|
2024-01-15 11:31:03 +00:00
|
|
|
*
|
2024-08-08 10:46:10 +00:00
|
|
|
* @param vExpeditions JSON_ARRAY with this structure [exp1, exp2, exp3, ...]
|
|
|
|
* @param vArcId INT Identifier of arcRead
|
|
|
|
* @param vWorkerFk INT Identifier of worker
|
|
|
|
* @param out vPalletFk Identifier of expeditionPallet
|
2024-01-15 11:31:03 +00:00
|
|
|
*/
|
2024-08-08 10:46:10 +00:00
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
DECLARE vCounter INT;
|
|
|
|
DECLARE vExpeditionFk INT;
|
|
|
|
DECLARE vTruckFk INT;
|
|
|
|
DECLARE vPrinterFk INT;
|
2024-07-22 08:07:21 +00:00
|
|
|
DECLARE vExpeditionStateTypeFk INT;
|
2024-08-08 10:46:10 +00:00
|
|
|
DECLARE vFreeExpeditionCount INT;
|
|
|
|
DECLARE vExpeditionWithPallet 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;
|
|
|
|
|
2024-08-08 10:46:10 +00:00
|
|
|
SELECT COUNT(expeditionFk) INTO vFreeExpeditionCount
|
|
|
|
FROM tExpedition
|
|
|
|
WHERE palletFk IS NULL;
|
|
|
|
|
2024-09-02 07:24:08 +00:00
|
|
|
SELECT COUNT(expeditionFk) INTO vExpeditionWithPallet
|
2024-07-25 13:03:33 +00:00
|
|
|
FROM tExpedition
|
|
|
|
WHERE palletFk;
|
|
|
|
|
2024-10-04 05:52:13 +00:00
|
|
|
IF vExpeditionWithPallet THEN
|
|
|
|
UPDATE arcRead
|
|
|
|
SET error = (
|
|
|
|
SELECT GROUP_CONCAT(expeditionFk SEPARATOR ', ')
|
|
|
|
FROM tExpedition
|
|
|
|
WHERE palletFk
|
|
|
|
)
|
|
|
|
WHERE id = vArcId;
|
|
|
|
LEAVE proc;
|
|
|
|
END IF;
|
|
|
|
|
2024-08-08 10:46:10 +00:00
|
|
|
IF NOT vFreeExpeditionCount THEN
|
2024-07-25 13:03:33 +00:00
|
|
|
CALL util.throw ('NO_FREE_EXPEDITIONS');
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
SELECT roadmapStopFk INTO vTruckFk
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM (
|
2024-07-25 13:03:33 +00:00
|
|
|
SELECT rm.roadmapStopFk, count(*) n
|
|
|
|
FROM routesMonitor rm
|
|
|
|
JOIN tExpedition e ON e.routeFk = rm.routeFk
|
|
|
|
WHERE e.palletFk IS NULL
|
|
|
|
GROUP BY roadmapStopFk
|
2024-07-18 13:14:18 +00:00
|
|
|
ORDER BY n DESC
|
2024-07-25 13:03:33 +00:00
|
|
|
LIMIT 1
|
|
|
|
) sub;
|
|
|
|
|
|
|
|
IF vTruckFk IS NULL THEN
|
|
|
|
CALL util.throw ('TRUCK_NOT_AVAILABLE');
|
2024-01-15 11:31:03 +00:00
|
|
|
END IF;
|
|
|
|
|
2024-07-25 13:03:33 +00:00
|
|
|
INSERT INTO expeditionPallet SET truckFk = vTruckFk;
|
|
|
|
|
|
|
|
SET vPalletFk = LAST_INSERT_ID();
|
|
|
|
|
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
|
2024-07-25 13:03:33 +00:00
|
|
|
WHERE palletFk IS NULL;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-07-22 08:07:21 +00:00
|
|
|
SELECT id INTO vExpeditionStateTypeFk
|
|
|
|
FROM expeditionStateType
|
|
|
|
WHERE code = 'PALLETIZED';
|
2024-07-25 13:03:33 +00:00
|
|
|
|
2024-07-18 13:14:18 +00:00
|
|
|
INSERT INTO expeditionState(expeditionFk, typeFk)
|
2024-07-25 13:03:33 +00:00
|
|
|
SELECT expeditionFk, vExpeditionStateTypeFk
|
2024-09-02 07:24:08 +00:00
|
|
|
FROM tExpedition
|
|
|
|
WHERE palletFk IS NULL;
|
2024-07-25 13:03:33 +00:00
|
|
|
|
2024-10-04 05:52:13 +00:00
|
|
|
UPDATE arcRead SET error = NULL WHERE id = vArcId;
|
2024-07-18 13:14:18 +00:00
|
|
|
|
2024-10-04 05:52:13 +00:00
|
|
|
SELECT printerFk INTO vPrinterFk FROM arcRead WHERE id = vArcId;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-10-04 05:52:13 +00:00
|
|
|
CALL report_print(
|
|
|
|
'LabelPalletExpedition',
|
|
|
|
vPrinterFk,
|
|
|
|
account.myUser_getId(),
|
|
|
|
JSON_OBJECT('palletFk', vPalletFk, 'userFk', account.myUser_getId()),
|
|
|
|
'high'
|
|
|
|
);
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-10-04 05:52:13 +00:00
|
|
|
UPDATE expeditionPallet SET isPrint = TRUE WHERE id = vPalletFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
DROP TEMPORARY TABLE tExpedition;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|