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

32 lines
647 B
MySQL
Raw Permalink Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`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 ;