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

55 lines
1.2 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-11-14 11:11:14 +00:00
DECLARE vCommission INT;
DECLARE vCurrentCommission INT;
2024-10-18 06:21:09 +00:00
DECLARE vIsNotEUR INT;
2024-11-14 11:11:14 +00:00
DECLARE vLastEntryFk INT;
DECLARE vLanded INT;
2024-11-14 11:11:14 +00:00
2024-10-18 06:21:09 +00:00
SELECT count(*) INTO vIsNotEUR
2024-11-14 11:11:14 +00:00
FROM currency c
2024-10-18 06:21:09 +00:00
WHERE c.code <> 'EUR' AND id = vCurrencyFk;
2024-11-14 11:11:14 +00:00
2024-10-18 06:21:09 +00:00
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 landed INTO vLanded
2024-11-14 11:11:14 +00:00
FROM travel
WHERE id = vTravelFk;
SELECT e.id INTO vLastEntryFk
2024-11-15 10:32:20 +00:00
FROM `entry` e
2024-11-14 11:11:14 +00:00
JOIN travel tr ON tr.id = e.travelFk
2024-11-26 10:57:24 +00:00
WHERE e.supplierFk = vSupplierFk
ORDER BY (vLanded <= tr.landed), tr.landed DESC
2024-11-14 11:11:14 +00:00
LIMIT 1;
IF vLastEntryFk THEN
SELECT commission INTO vCurrentCommission
2024-11-15 10:32:20 +00:00
FROM `entry`
2024-11-14 11:11:14 +00:00
WHERE id = vLastEntryFk;
ELSE
SELECT commission INTO vCurrentCommission
FROM supplier s
WHERE s.id = vSupplierFk;
END IF;
2024-11-14 11:11:14 +00:00
RETURN vCurrentCommission;
2024-10-18 06:21:09 +00:00
END IF;
END$$
DELIMITER ;