salix/db/routines/vn/functions/entry_getCommission.sql

52 lines
1.1 KiB
MySQL
Raw Normal View History

DELIMITER $$
2024-08-20 08:06:10 +00:00
CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`entry_getCommission`(vTravelFk INT,
2024-10-18 06:21:09 +00:00
vCurrencyFk INT,
vSupplierFk INT
)
RETURNS int(11)
NOT DETERMINISTIC
READS SQL DATA
BEGIN
2024-10-18 06:21:09 +00:00
DECLARE vCommission INT;
DECLARE vCurrentCommission INT;
DECLARE vIsNotEUR INT;
DECLARE vLastEntryFk INT;
SELECT count(*) INTO vIsNotEUR
FROM currency c
WHERE c.code <> 'EUR' AND id = vCurrencyFk;
IF vIsNotEUR THEN
SELECT ROUND(-100 * (1 - (1 / r.value))) INTO vCommission
FROM travel t
LEFT JOIN referenceCurrent r ON r.currencyFk = vCurrencyFk AND r.`dated` <= t.shipped
WHERE t.id = vTravelFk
ORDER BY r.`dated` DESC
LIMIT 1;
RETURN IFNULL(vCommission, 0);
ELSE
SELECT e.id INTO vLastEntryFk
2024-11-15 10:32:20 +00:00
FROM `entry` e
JOIN travel tr ON tr.id = e.travelFk
2024-10-18 06:21:09 +00:00
WHERE e.supplierFk = vSupplierFk
ORDER BY tr.landed DESC
LIMIT 1;
IF vLastEntryFk THEN
SELECT commission INTO vCurrentCommission
2024-11-15 10:32:20 +00:00
FROM `entry`
2024-10-18 06:21:09 +00:00
WHERE id = vLastEntryFk;
ELSE
SELECT commission INTO vCurrentCommission
FROM supplier s
WHERE s.id = vSupplierFk;
2024-10-18 06:21:09 +00:00
END IF;
2024-10-18 06:21:09 +00:00
RETURN vCurrentCommission;
END IF;
END$$
DELIMITER ;