salix/db/routines/vn/procedures/rateView.sql

43 lines
1.2 KiB
MySQL
Raw Normal View History

2024-03-05 11:41:54 +00:00
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`rateView`()
BEGIN
DECLARE v10Years DATE DEFAULT util.VN_CURDATE() - INTERVAL 10 YEAR;
SELECT
t.year,
t.month,
pagos.dollars,
pagos.changePractical,
CAST(SUM(iit.foreignValue ) / SUM(iit.taxableBase) AS DECIMAL(5,4)) cambioTeorico,
pagos.changeOfficial
FROM invoiceIn ii
JOIN time t ON t.dated = ii.issued
JOIN invoiceInTax iit ON ii.id = iit.invoiceInFk
JOIN
(
SELECT
t.year,
t.month,
CAST(SUM(divisa) AS DECIMAL(10,2)) dollars,
CAST(SUM(divisa) / SUM(amount) AS DECIMAL(5,4)) changePractical,
CAST(rr.value * 0.998 AS DECIMAL(5,4)) changeOfficial
FROM payment p
JOIN time t ON t.dated = p.received
JOIN referenceRate rr ON rr.dated = p.received
JOIN currency c ON c.id = rr.currencyFk
WHERE divisa
AND p.received >= v10Years
AND c.code = 'USD'
GROUP BY t.year, t.month
) pagos ON t.year = pagos.year AND t.month = pagos.MONTH
JOIN currency c ON c.id = ii.currencyFk
WHERE c.code = 'USD'
AND ii.issued >= v10Years
AND iit.foreignValue
AND iit.taxableBase
GROUP BY t.year, t.month;
END$$
DELIMITER ;