6444-MigrateVn2008.intrastat_netoFunctionToVn #1972

Merged
ivanm merged 10 commits from 6444-MigrateVn2008.intrastat_netoFunctionToVn into dev 2024-02-06 13:21:24 +00:00
2 changed files with 32 additions and 20 deletions

View File

@ -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 ;

View File

@ -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 ;