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,
|
2024-02-06 07:40:06 +00:00
|
|
|
vStems INT
|
2024-02-05 07:32:45 +00:00
|
|
|
)
|
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
|
2024-02-06 07:40:06 +00:00
|
|
|
* @param vStems Número de unidades
|
2024-02-05 07:32:45 +00:00
|
|
|
* @return vNet
|
|
|
|
*/
|
2024-02-01 12:02:03 +00:00
|
|
|
DECLARE vNet DOUBLE;
|
2024-01-30 08:45:37 +00:00
|
|
|
|
2024-02-06 07:40:06 +00:00
|
|
|
SELECT ROUND(vStems / (SUM(average) / COUNT(average)), 2) INTO vNet
|
2024-02-05 07:32:45 +00:00
|
|
|
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 ;
|