salix/db/routines/bs/procedures/manaSpellers_actualize.sql

30 lines
885 B
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bs`.`manaSpellers_actualize`()
BEGIN
/**
* Recalcula el valor del campo con el modificador de precio
* para el componente de maná automático.
*/
UPDATE vn.departmentMana dm
JOIN (
SELECT c.lastSalesDepartmentFk,
FLOOR(SUM(s.amount) / 12) amount
FROM salesByClientDepartment s
JOIN vn.client c ON c.id = s.clientFk
WHERE s.dated BETWEEN util.VN_CURDATE() - INTERVAL 1 YEAR AND util.VN_CURDATE()
GROUP BY c.lastSalesDepartmentFk
)avgPortfolioWeight ON avgPortfolioWeight.lastSalesDepartmentFk = dm.salesDepartmentFk
JOIN vn.salesDepartmentConfig sdc
SET dm.pricesModifierRate =
IFNULL(
GREATEST(
sdc.manaMinRate,
LEAST(
sdc.manaMaxRate,
ROUND( - dm.amount / avgPortfolioWeight.amount, 3)
)
)
,0);
END$$
DELIMITER ;