salix/db/routines/vn/procedures/ektEntryAssign_setEntry.sql

113 lines
3.1 KiB
MySQL
Raw Normal View History

DELIMITER $$
2024-08-20 08:06:10 +00:00
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`ektEntryAssign_setEntry`()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE vEntryFk INT;
DECLARE vTravelFk INT;
DECLARE vId INT;
DECLARE vLanded DATE DEFAULT util.VN_CURDATE();
DECLARE cur1 CURSOR FOR SELECT id FROM vn.ektEntryAssign;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
IF HOUR(util.VN_NOW()) > 12 THEN
SET vLanded = TIMESTAMPADD(DAY,1,vLanded);
END IF;
OPEN cur1;
read_loop: LOOP
SET done = FALSE;
SET vEntryFk = 0;
FETCH cur1 INTO vId;
IF done THEN
LEAVE read_loop;
END IF;
SELECT IFNULL(e.id,0) INTO vEntryFk
FROM vn.entry e
JOIN vn.travel tr ON tr.id = e.travelFk
JOIN vn.ektEntryAssign ea ON ea.warehouseOutFk = tr.warehouseOutFk
AND ea.warehouseInFk = tr.warehouseInFk
AND ea.agencyModeFk = tr.agencyModeFk
AND ea.supplierFk = e.supplierFk
WHERE ea.id = vId
AND tr.landed = vLanded
LIMIT 1;
IF NOT vEntryFk THEN
SELECT IFNULL(e.id,0) INTO vEntryFk
FROM vn.entry e
JOIN vn.travel tr ON tr.id = e.travelFk
JOIN vn.ektEntryAssign ea ON ea.warehouseOutFk = tr.warehouseOutFk
AND ea.warehouseInFk = tr.warehouseInFk
AND ea.agencyModeFk = tr.agencyModeFk
WHERE ea.id = vId
AND tr.landed = vLanded
AND ea.supplierFk IS NULL
LIMIT 1;
END IF;
IF NOT vEntryFk THEN
SET vTravelFk = 0;
SELECT IFNULL(MAX(tr.id),0) INTO vTravelFk
FROM vn.travel tr
JOIN vn.ektEntryAssign ea ON ea.warehouseOutFk = tr.warehouseOutFk
AND ea.warehouseInFk = tr.warehouseInFk
AND ea.agencyModeFk = tr.agencyModeFk
WHERE ea.id = vId
AND tr.landed = vLanded;
IF NOT vTravelFk THEN
INSERT INTO vn.travel(landed, shipped, warehouseInFk, warehouseOutFk, agencyModeFk)
SELECT vLanded, vLanded, warehouseInFk, warehouseOutFk, agencyModeFk
FROM vn.ektEntryAssign
WHERE id = vId;
SELECT MAX(tr.id) INTO vTravelFk
FROM vn.travel tr
JOIN vn.ektEntryAssign ea ON ea.warehouseOutFk = tr.warehouseOutFk
AND ea.warehouseInFk = tr.warehouseInFk
2024-01-25 16:33:54 +00:00
AND ea.agencyModeFk = tr.agencyModeFk
WHERE ea.id = vId
AND tr.landed = vLanded;
END IF;
INSERT INTO vn.entry(travelFk, supplierFk, companyFk, currencyFk)
SELECT vTravelFk, IFNULL(ea.supplierFk,s.id), c.id, cu.id
FROM vn.supplier s
JOIN vn.company c ON c.code = 'VNL'
JOIN vn.currency cu ON cu.code = 'EUR'
JOIN vn.ektEntryAssign ea ON ea.id = vId
WHERE s.name = 'KONINKLIJE COOPERATIEVE BLOEMENVEILING FLORAHOLLAN';
SELECT MAX(id) INTO vEntryFk
FROM vn.entry
WHERE travelFk = vTravelFk;
END IF;
UPDATE vn.ektEntryAssign
SET entryFk = vEntryFk
WHERE id = vId;
END LOOP;
CLOSE cur1;
END$$
DELIMITER ;