2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-07-22 09:52:42 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` FUNCTION `vn`.`entry_getCommission`(vTravelFk INT,
|
2024-01-15 11:31:03 +00:00
|
|
|
vCurrencyFk INT,
|
|
|
|
vSupplierFk INT
|
|
|
|
)
|
|
|
|
RETURNS int(11)
|
|
|
|
NOT DETERMINISTIC
|
|
|
|
READS SQL DATA
|
|
|
|
BEGIN
|
|
|
|
DECLARE vCommission INT;
|
|
|
|
DECLARE vCurrentCommission INT;
|
|
|
|
DECLARE vIsCurrencyUsd INT;
|
|
|
|
DECLARE vLastEntryFk INT;
|
|
|
|
|
|
|
|
SELECT count(*) INTO vIsCurrencyUsd
|
|
|
|
FROM currency c
|
|
|
|
WHERE c.code = 'USD' AND id = vCurrencyFk;
|
|
|
|
|
|
|
|
IF NOT vIsCurrencyUsd THEN
|
|
|
|
|
|
|
|
SELECT e.id INTO vLastEntryFk
|
|
|
|
FROM vn.entry e
|
|
|
|
JOIN vn.travel tr ON tr.id = e.travelFk
|
|
|
|
WHERE e.supplierFk = vSupplierFk
|
|
|
|
ORDER BY tr.landed DESC
|
|
|
|
LIMIT 1;
|
|
|
|
|
|
|
|
IF vLastEntryFk THEN
|
|
|
|
|
|
|
|
SELECT commission INTO vCurrentCommission
|
|
|
|
FROM vn.entry
|
|
|
|
WHERE id = vLastEntryFk;
|
|
|
|
|
|
|
|
ELSE
|
|
|
|
|
|
|
|
SELECT commission INTO vCurrentCommission
|
|
|
|
FROM supplier s
|
|
|
|
WHERE s.id = vSupplierFk;
|
|
|
|
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
RETURN vCurrentCommission;
|
|
|
|
|
|
|
|
ELSE
|
|
|
|
|
|
|
|
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 IF;
|
|
|
|
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|