-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unificar dados do T4 com dado anual
- Loading branch information
Showing
6 changed files
with
76 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 49 additions & 67 deletions
116
pkg/contabil/repositorio/repositorio_sqlite_trimestral.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,53 @@ | ||
WITH CalculatedValues AS ( | ||
WITH | ||
acumulado AS ( | ||
SELECT codigo, descr, data_ini_exerc, data_fim_exerc, SUBSTR(c.data_fim_exerc, 1, 4) ano, meses, | ||
SUM(CASE | ||
WHEN meses = 3 THEN valor | ||
WHEN meses = 12 AND SUBSTR(c.data_fim_exerc, 6, 2) = '03' THEN valor | ||
ELSE NULL END) AS q1, | ||
SUM(CASE | ||
WHEN meses = 6 THEN valor | ||
WHEN meses = 12 AND SUBSTR(c.data_fim_exerc, 6, 2) = '06' THEN valor | ||
ELSE NULL END) AS q2, | ||
SUM(CASE | ||
WHEN meses = 9 THEN valor | ||
WHEN meses = 12 AND SUBSTR(c.data_fim_exerc, 6, 2) = '09' THEN valor | ||
ELSE NULL END) AS q3, | ||
SUM(CASE WHEN data_ini_exerc <> '' AND meses = 12 THEN valor ELSE NULL END) AS q4, | ||
SUM(CASE WHEN meses = 12 AND SUBSTR(c.data_fim_exerc, 6, 2) = '12' THEN valor ELSE NULL END) AS q4_anual | ||
FROM | ||
empresas e | ||
JOIN contas c ON e.id = c.id_empresa | ||
WHERE c.id_empresa IN (%s) | ||
AND c.consolidado = %d | ||
AND (c.data_ini_exerc = '' OR SUBSTR(c.data_ini_exerc, 6, 2) = "01") -- APENAS data_ini_exec DE JANEIRO | ||
GROUP BY ano, codigo, descr | ||
ORDER BY data_fim_exerc | ||
), | ||
calculado AS ( | ||
SELECT | ||
ano, | ||
codigo, | ||
descr, | ||
COALESCE(q1, 0) AS t1, | ||
CASE WHEN data_ini_exerc <> '' AND q1 IS NOT NULL AND q2 IS NOT NULL THEN q2-q1 ELSE COALESCE(q2, 0) END AS t2, | ||
CASE WHEN data_ini_exerc <> '' AND q2 IS NOT NULL AND q3 IS NOT NULL THEN q3-q2 ELSE COALESCE(q3, 0) END AS t3, | ||
CASE WHEN data_ini_exerc <> '' AND q4 IS NOT NULL THEN q4-COALESCE(q3, 0) ELSE COALESCE(q4_anual, 0) END AS t4 | ||
FROM acumulado | ||
), | ||
agrupado AS ( | ||
SELECT | ||
year, | ||
codigo, | ||
descr, | ||
COALESCE(q1_value, 0) q1_value, | ||
COALESCE(q2_value, 0) q2_value, | ||
COALESCE(q3_value, 0) q3_value, | ||
CASE WHEN q1_value IS NOT NULL AND q2_value IS NOT NULL AND q3_value IS NOT NULL | ||
THEN yearly_value - (q1_value + q2_value + q3_value) | ||
ELSE 0 | ||
END AS q4_calculated, | ||
CASE WHEN q1_value IS NOT NULL AND q2_value IS NOT NULL AND q3_value IS NOT NULL | ||
THEN COALESCE(yearly_value, 0) | ||
ELSE CASE WHEN SUBSTR(data_fim_exerc, 6, 2) IN ('10', '11', '12') | ||
THEN COALESCE(yearly_value, 0) | ||
ELSE 0 | ||
END | ||
END AS yearly_value | ||
FROM ( | ||
SELECT | ||
CASE WHEN c.data_ini_exerc <> '' THEN SUBSTR(c.data_ini_exerc, 1, 4) | ||
ELSE SUBSTR(c.data_fim_exerc, 1, 4) | ||
END AS year, | ||
c.data_fim_exerc, | ||
c.codigo, | ||
c.descr, | ||
SUM(CASE | ||
WHEN meses = 3 AND SUBSTR(c.data_ini_exerc, 6, 2) IN ('01', '02', '03') THEN valor | ||
WHEN meses = 12 AND SUBSTR(c.data_fim_exerc, 6, 2) IN ('01', '02', '03') THEN valor | ||
ELSE NULL END) AS q1_value, | ||
SUM(CASE | ||
WHEN meses = 3 AND SUBSTR(c.data_ini_exerc, 6, 2) IN ('04', '05', '06') THEN valor | ||
WHEN meses = 12 AND SUBSTR(c.data_fim_exerc, 6, 2) IN ('04', '05', '06') THEN valor | ||
ELSE NULL END) AS q2_value, | ||
SUM(CASE | ||
WHEN meses = 3 AND SUBSTR(c.data_ini_exerc, 6, 2) IN ('07', '08', '09') THEN valor | ||
WHEN meses = 12 AND SUBSTR(c.data_fim_exerc, 6, 2) IN ('07', '08', '09') THEN valor | ||
ELSE NULL END) AS q3_value, | ||
(CASE | ||
WHEN meses = 12 AND c.data_fim_exerc = MAX(c.data_fim_exerc) THEN valor | ||
ELSE NULL END) AS yearly_value | ||
|
||
FROM | ||
empresas e | ||
JOIN contas c ON e.id = c.id_empresa | ||
WHERE c.id_empresa IN (%s) | ||
AND c.consolidado = %d | ||
GROUP BY | ||
CASE WHEN c.data_ini_exerc <> '' THEN SUBSTR(c.data_ini_exerc, 1, 4) | ||
ELSE SUBSTR(c.data_fim_exerc, 1, 4) | ||
END, | ||
c.codigo, | ||
c.descr | ||
ORDER BY data_fim_exerc | ||
) | ||
ORDER BY year, codigo | ||
'[' || GROUP_CONCAT( | ||
'{"ano":' || ano || | ||
',"t1":' || COALESCE(t1, 0) || | ||
',"t2":' || COALESCE(t2, 0) || | ||
',"t3":' || COALESCE(t3, 0) || | ||
',"t4":' || COALESCE(t4, 0) || '}' | ||
) || ']' AS valores | ||
FROM calculado | ||
WHERE t1 <> 0 OR t2 <> 0 OR t3 <> 0 OR t4 <> 0 -- FILTRA LINHAS VAZIAS | ||
GROUP BY codigo, descr | ||
) | ||
SELECT | ||
codigo, | ||
descr, | ||
'[' || GROUP_CONCAT( | ||
'{"year":' || year || | ||
',"q1":' || COALESCE(q1_value, 0) || | ||
',"q2":' || COALESCE(q2_value, 0) || | ||
',"q3":' || COALESCE(q3_value, 0) || | ||
',"q4":' || COALESCE(q4_calculated, 0) || | ||
',"yearly":' || COALESCE(yearly_value, 0) || '}' | ||
) || ']' AS valores | ||
FROM CalculatedValues | ||
GROUP BY codigo, descr | ||
SELECT * from agrupado |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters