From 7e0f19b368c796def55b3ebcb070535ff79c2f78 Mon Sep 17 00:00:00 2001 From: robert Date: Fri, 18 Oct 2024 08:21:09 +0200 Subject: [PATCH 1/2] feat: refs #8127 entry_getCommission --- .../vn/functions/entry_getCommission.sql | 78 +++++++++---------- .../vn/procedures/entry_updateComission.sql | 4 +- 2 files changed, 39 insertions(+), 43 deletions(-) diff --git a/db/routines/vn/functions/entry_getCommission.sql b/db/routines/vn/functions/entry_getCommission.sql index 4a19f4e63..898ac8521 100644 --- a/db/routines/vn/functions/entry_getCommission.sql +++ b/db/routines/vn/functions/entry_getCommission.sql @@ -1,58 +1,54 @@ DELIMITER $$ CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`entry_getCommission`(vTravelFk INT, - vCurrencyFk INT, - vSupplierFk INT + 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 + DECLARE vCommission INT; + DECLARE vCurrentCommission INT; + DECLARE vIsNotEUR INT; + DECLARE vLastEntryFk INT; + SELECT count(*) INTO vIsNotEUR + FROM currency c + WHERE c.code <> 'EUR' AND id = vCurrencyFk; + + 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); - - END IF; - + + ELSE + 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; + + END IF; + END$$ DELIMITER ; diff --git a/db/routines/vn/procedures/entry_updateComission.sql b/db/routines/vn/procedures/entry_updateComission.sql index e2de2a4a5..8db28f97d 100644 --- a/db/routines/vn/procedures/entry_updateComission.sql +++ b/db/routines/vn/procedures/entry_updateComission.sql @@ -22,7 +22,7 @@ BEGIN FROM vn.entry e JOIN vn.travel t ON t.id = e.travelFk JOIN vn.warehouse w ON w.id = t.warehouseInFk - WHERE t.shipped >= util.VN_CURDATE() + WHERE t.landed >= util.VN_CURDATE() AND e.currencyFk = vCurrency AND NOT e.isBooked; @@ -33,7 +33,7 @@ BEGIN SET e.commission = vComission; SELECT `name` INTO vCurrencyName - FROM currency + FROM currency WHERE id = vCurrency; CALL entry_recalc(); -- 2.40.1 From c5f1b05671c6fefaa22df376af105589dd60b402 Mon Sep 17 00:00:00 2001 From: robert Date: Fri, 15 Nov 2024 11:32:20 +0100 Subject: [PATCH 2/2] feat: refs #8127 quitar esquemas --- db/routines/vn/functions/entry_getCommission.sql | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/db/routines/vn/functions/entry_getCommission.sql b/db/routines/vn/functions/entry_getCommission.sql index 898ac8521..a4afdabd4 100644 --- a/db/routines/vn/functions/entry_getCommission.sql +++ b/db/routines/vn/functions/entry_getCommission.sql @@ -25,18 +25,17 @@ BEGIN LIMIT 1; RETURN IFNULL(vCommission, 0); - ELSE SELECT e.id INTO vLastEntryFk - FROM vn.entry e - JOIN vn.travel tr ON tr.id = e.travelFk + FROM `entry` e + JOIN 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 + FROM `entry` WHERE id = vLastEntryFk; ELSE @@ -47,8 +46,6 @@ BEGIN END IF; RETURN vCurrentCommission; - END IF; - END$$ DELIMITER ; -- 2.40.1