CREATE OR REPLACE FUNCTION public.fnc_geraldia_pivot (
  parinicio date,
  parfinal date
)
RETURNS varchar AS
$body$
DECLARE
   strCategorias   TEXT;
   strSQL          TEXT;
   strColunas	   TEXT;
BEGIN
  
   BEGIN
        SELECT coalesce(STRING_AGG(distinct '"C_'|| C.DESCRICAO||'" text',','),'') as cat,
            	   coalesce(STRING_AGG(distinct 'COALESCE("C_'|| C.DESCRICAO|| '",''0,00'') AS "C_'|| C.DESCRICAO||'"',','),'') as COLUNA
            into strCategorias,strColunas
              FROM controle_despesas_operacionais CDO
                  ,CATEGORIA C
             WHERE C.CODIGO = CDO.chave_categoria
               AND CDO.DATA BETWEEN parinicio and parfinal;
   EXCEPTION
   	    WHEN NO_DATA_FOUND THEN
        	 strCategorias := '';
   END;

   strSQL := 'SELECT row_number() over() as CHAVE
                    ,CDO."DATA" AS DT
                    ,(SELECT coalesce(replace(to_char(sum(crf.valor_liquido),''FM999999999.00''),''.'','',''),''0,00'') FROM CONTROLE_RECEITAS_FRETES crf WHERE crf.DATA = CDO."DATA") AS VALOR_FRETE
                    ,coalesce(replace(to_char(cast((SELECT sum(crf.valor_liquido) FROM CONTROLE_RECEITAS_FRETES crf WHERE crf.DATA = CDO."DATA") - (Select sum(cd.valor) from controle_despesas_operacionais cd where cd.data = CDO."DATA") as numeric(15,2)),''FM999999999.00''),''.'','',''),''0,00'') as liquido
					,'||strColunas||'
                FROM (SELECT CR.* FROM (SELECT *
			    FROM crosstab(''SELECT CDO.DATA, CDO.CHAVE_CATEGORIA, coalesce(replace(to_char(sum(cdo.valor),''''FM999999999.00''''),''''.'''','''',''''),''''0,00'''')
                                FROM CONTROLE_DESPESAS_OPERACIONAIS CDO
                                 WHERE CDO.DATA BETWEEN '''''||parinicio||''''' and '''''||parfinal||''''' group by cdo.data,cdo.chave_categoria ORDER BY 1,2'',''select distinct chave_categoria from controle_despesas_operacionais WHERE DATA BETWEEN '''''||parinicio||''''' and '''''||parfinal||''''' order by 1'')
                                 AS ct("DATA" date, '||strCategorias||')) CR) CDO
   				union all
                SELECT CAST(''0'' AS INTEGER) as CHAVE
                    ,to_date(''01/01/1900'',''dd/MM/yyyy'') AS DT
                    ,coalesce(replace(to_char(cast((SELECT sum(crf.valor_liquido) FROM CONTROLE_RECEITAS_FRETES crf WHERE crf.DATA BETWEEN '''||parinicio||''' and '''||parfinal||''' ) as numeric(15,2)),''FM999999999.00''),''.'','',''),''0,00'') AS VALOR_FRETE
                    ,coalesce(replace(to_char(cast((SELECT sum(crf.valor_liquido) FROM CONTROLE_RECEITAS_FRETES crf WHERE crf.DATA BETWEEN '''||parinicio||''' and '''||parfinal||''') - (Select sum(cd.valor) from controle_despesas_operacionais cd where cd.data BETWEEN '''||parinicio||''' and '''||parfinal||''') as numeric(15,2)),''FM999999999.00''),''.'','',''),''0,00'') as liquido
                    ,'||strColunas||'
                FROM (SELECT CR.* FROM (SELECT *
			    FROM crosstab(''SELECT to_date(''''01/01/1900'''',''''dd/MM/yyyy''''), CDO.CHAVE_CATEGORIA,  coalesce(replace(to_char(sum(cdo.valor),''''FM999999999.00''''),''''.'''','''',''''),''''0,00'''')
                                FROM CONTROLE_DESPESAS_OPERACIONAIS CDO
                                 WHERE CDO.DATA BETWEEN '''''||parinicio||''''' and '''''||parfinal||''''' group by cdo.chave_categoria ORDER BY 1,2'',''select distinct chave_categoria from controle_despesas_operacionais where DATA BETWEEN '''''||parinicio||''''' and '''''||parfinal||''''' order by 1'')
                                 AS ct("DATA" date, '|| strCategorias ||')) CR) CDO order by 1';

   return strSQL;

END;
$body$
LANGUAGE 'plpgsql'
VOLATILE
CALLED ON NULL INPUT
SECURITY INVOKER
COST 100;