38 lines
983 B
SQL
38 lines
983 B
SQL
DROP function IF EXISTS `vn`.`entry_getCommission`;
|
|
|
|
DELIMITER $$
|
|
CREATE DEFINER=`root`@`%` FUNCTION `vn`.`entry_getCommission`(
|
|
vTravelFk INT,
|
|
vCurrencyFk INT,
|
|
vSupplierFk INT
|
|
) RETURNS int(11)
|
|
READS SQL DATA
|
|
BEGIN
|
|
DECLARE vCommission INT;
|
|
DECLARE vCurrentCommission INT;
|
|
DECLARE vIsCurrencyUsd INT;
|
|
|
|
SELECT count(*) INTO vIsCurrencyUsd
|
|
FROM currency c
|
|
WHERE c.code = 'USD' AND id = vCurrencyFk;
|
|
|
|
IF NOT vIsCurrencyUsd THEN
|
|
SELECT commission INTO vCurrentCommission
|
|
FROM supplier s
|
|
WHERE s.id = vSupplierFk;
|
|
RETURN vCurrentCommission;
|
|
END IF;
|
|
|
|
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);
|
|
END$$
|
|
|
|
DELIMITER ;
|
|
|