salix/db/routines/stock/procedures/outbound_requestQuantity.sql

62 lines
1.4 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `stock`.`outbound_requestQuantity`(
vSelf INT,
vRequested INT,
vDated DATETIME,
OUT vSupplied INT)
BEGIN
/**
* Disassociates outbound picks after the given date until the
* demanded quantity is satisfied.
*
* @param vSelf The outbound reference
* @param vRequested The requested quantity
* @param vDate The starting date for the associated inbounds
* @param vSupplied The supplied quantity
*/
DECLARE vInboundFk INT;
DECLARE vPickQuantity INT;
DECLARE vPickGranted INT;
DECLARE vDone BOOL;
DECLARE vPicks CURSOR FOR
SELECT p.inboundFk, p.quantity
FROM inboundPick p
JOIN inbound i ON i.id = p.inboundFk
WHERE p.outboundFk = vSelf
AND i.dated > vDated
ORDER BY i.dated DESC;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET vDone = TRUE;
SET vSupplied = 0;
OPEN vPicks;
myLoop: LOOP
SET vDone = FALSE;
FETCH vPicks INTO vInboundFk, vPickQuantity;
IF vDone THEN
LEAVE myLoop;
END IF;
SET vPickGranted = LEAST(vRequested - vSupplied, vPickQuantity);
SET vSupplied = vSupplied + vPickGranted;
CALL inbound_removePick(vInboundFk, vSelf, vPickGranted, vPickQuantity);
UPDATE inbound
SET isSync = FALSE,
available = available + vPickGranted
WHERE id = vInboundFk;
IF vSupplied >= vRequested THEN
LEAVE myLoop;
END IF;
END LOOP;
CLOSE vPicks;
END$$
DELIMITER ;