diff --git a/db/routines/vn/functions/intrastat_estimateNet.sql b/db/routines/vn/functions/intrastat_estimateNet.sql new file mode 100644 index 000000000..350cb788a --- /dev/null +++ b/db/routines/vn/functions/intrastat_estimateNet.sql @@ -0,0 +1,32 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`intrastat_estimateNet`( + vSelf INT, + 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 +* @param vStems Número de unidades +* @return vNet +*/ + DECLARE vNet DOUBLE; + + 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 ; \ No newline at end of file diff --git a/db/routines/vn2008/functions/intrastat_neto.sql b/db/routines/vn2008/functions/intrastat_neto.sql deleted file mode 100644 index 34e01de8c..000000000 --- a/db/routines/vn2008/functions/intrastat_neto.sql +++ /dev/null @@ -1,20 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn2008`.`intrastat_neto`(intINSTRASTAT INTEGER,intUNIDADES INTEGER) - RETURNS double - DETERMINISTIC -BEGIN - - DECLARE n DOUBLE; - - SELECT ROUND(intUNIDADES / (SUM(MEDIA) / COUNT(media)), 2) INTO n FROM - (SELECT *, unidades / neto MEDIA - FROM intrastat_data - WHERE intrastat_id = intINSTRASTAT AND neto - AND unidades > 0 - ORDER BY odbc_date DESC - LIMIT 20) t; - -- JGF 01/06 per a evitar Kg en negatiu - RETURN n/2; - -END$$ -DELIMITER ;