2024-01-30 08:45:37 +00:00
|
|
|
DELIMITER $$
|
2024-02-05 07:32:45 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`intrastat_estimateNet`(
|
|
|
|
vSelf INT,
|
|
|
|
vUnits INT
|
|
|
|
)
|
2024-01-30 08:45:37 +00:00
|
|
|
RETURNS double
|
|
|
|
DETERMINISTIC
|
|
|
|
BEGIN
|
2024-02-05 07:32:45 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2024-02-01 12:02:03 +00:00
|
|
|
DECLARE vNet DOUBLE;
|
2024-01-30 08:45:37 +00:00
|
|
|
|
2024-02-05 07:32:45 +00:00
|
|
|
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;
|
2024-02-01 12:02:03 +00:00
|
|
|
|
|
|
|
RETURN vNet/2;
|
2024-01-30 08:45:37 +00:00
|
|
|
END$$
|
|
|
|
DELIMITER ;
|