salix/db/changes/10120-december/00-entryGetComission.sql

38 lines
983 B
MySQL
Raw Normal View History

2019-11-26 13:15:43 +00:00
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 ;