diff --git a/db/routines/vn/functions/intrastat_estimateNet.sql b/db/routines/vn/functions/intrastat_estimateNet.sql index 99a52ded8..3ee1ca80f 100644 --- a/db/routines/vn/functions/intrastat_estimateNet.sql +++ b/db/routines/vn/functions/intrastat_estimateNet.sql @@ -1,21 +1,32 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`intrastat_estimateNet`(vIntrastat INT, vUnidades INT) +CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`intrastat_estimateNet`( + vSelf INT, + vUnits 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 +* @param vUnits Número de unidades +* @return vNet +*/ DECLARE vNet DOUBLE; - SELECT ROUND(vUnidades / (SUM(media) / COUNT(media)), 2) INTO vNet - FROM (SELECT *, unidades / neto media - FROM vn2008.intrastat_data - WHERE intrastat_id = vIntrastat - AND neto - AND unidades > 0 - ORDER BY odbc_date DESC - LIMIT 20) t; + SELECT ROUND(vUnits / (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 ; \ No newline at end of file