From 69314171ea7c8425343dca993c2de5b8b79c138d Mon Sep 17 00:00:00 2001 From: robert Date: Thu, 14 Nov 2024 12:11:14 +0100 Subject: [PATCH] feat: refs #8190 entry_getCommission --- .../vn/functions/entry_getCommission.sql | 67 ++++++++++--------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/db/routines/vn/functions/entry_getCommission.sql b/db/routines/vn/functions/entry_getCommission.sql index 4a19f4e63..0d294fe24 100644 --- a/db/routines/vn/functions/entry_getCommission.sql +++ b/db/routines/vn/functions/entry_getCommission.sql @@ -7,41 +7,46 @@ CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`entry_getCommission`(v 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 - + DECLARE vCommission INT; + DECLARE vCurrentCommission INT; + DECLARE vIsCurrencyUsd INT; + DECLARE vLastEntryFk INT; + DECLARE vTravelFkLanded INT; + + SELECT count(*) INTO vIsCurrencyUsd + FROM currency c + WHERE c.code = 'USD' AND id = vCurrencyFk; + + IF NOT vIsCurrencyUsd THEN + SELECT landed INTO vTravelFkLanded + FROM travel + WHERE id = vTravelFk; + 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 - + FROM entry e + JOIN travel tr ON tr.id = e.travelFk + WHERE tr.landed < vTravelFkLanded + AND e.supplierFk = vSupplierFk + ORDER BY (vTravelFkLanded - tr.landed) ASC, tr.landed DESC + LIMIT 1; + + IF vLastEntryFk THEN + SELECT commission INTO vCurrentCommission FROM vn.entry - WHERE id = vLastEntryFk; - - ELSE - + WHERE id = vLastEntryFk; + + ELSE + SELECT commission INTO vCurrentCommission FROM supplier s WHERE s.id = vSupplierFk; - + END IF; - - RETURN vCurrentCommission; - - ELSE + + RETURN vCurrentCommission; + + ELSE SELECT ROUND(-100 * (1 - (1 / r.value))) INTO vCommission FROM travel t @@ -49,10 +54,10 @@ BEGIN WHERE t.id = vTravelFk ORDER BY r.`dated` DESC LIMIT 1; - + RETURN IFNULL(vCommission, 0); - + END IF; - + END$$ DELIMITER ;