salix/db/routines/vn/functions/intrastat_estimateNet.sql

32 lines
645 B
MySQL
Raw Normal View History

DELIMITER $$
2024-08-20 08:06:10 +00:00
CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`intrastat_estimateNet`(
vSelf INT,
2024-02-06 07:40:06 +00:00
vStems INT
)
RETURNS double
DETERMINISTIC
BEGIN
/**
* Calcula un valor neto estimado en función de
* datos históricos de facturas intrastat.
*
* @param vSelf Id de intrastat
2024-02-06 07:40:06 +00:00
* @param vStems Número de unidades
* @return vNet
*/
DECLARE vNet DOUBLE;
2024-02-06 07:40:06 +00:00
SELECT ROUND(vStems / (SUM(average) / COUNT(average)), 2) INTO vNet
FROM (
SELECT *, stems / net average
FROM invoiceInIntrastat
WHERE intrastatFk = vSelf
AND net
AND stems > 0
ORDER BY dated DESC
LIMIT 20
) sub;
RETURN vNet/2;
END$$
DELIMITER ;