CREATE OR REPLACE FUNCTION public.fnc_geralplaca_pivot (
  parinicio date,
  parfinal date,
  parveiculo integer
)
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 := '';
             strColunas := '';
   END;
	if parveiculo is null then
       strSQL := 'SELECT row_number() over() as CHAVE
                        ,(Select v.placa from veiculo v where v.codigo = CDO."chave_veiculo") AS placa
                        ,(SELECT coalesce(replace(to_char(sum(crf.valor_liquido),''FM999999999.00''),''.'','',''),''0,00'') FROM CONTROLE_RECEITAS_FRETES crf WHERE crf.chave_veiculo = CDO."chave_veiculo" and crf.DATA BETWEEN '''||parinicio||''' and '''||parfinal||''') AS VALOR_FRETE
                        ,coalesce(replace(to_char(cast((SELECT sum(crf.valor_liquido) FROM CONTROLE_RECEITAS_FRETES crf WHERE crf.chave_veiculo = CDO."chave_veiculo" and crf.DATA BETWEEN '''||parinicio||''' and '''||parfinal||''') - (Select sum(cd.valor) from controle_despesas_operacionais cd where cd.chave_veiculo = CDO."chave_veiculo" and cd.DATA BETWEEN '''||parinicio||''' and '''||parfinal||''') as numeric(15,2)),''FM999999999.00''),''.'','',''),''0,00'') as liquido
                        ,CDO.CHAVE_VEICULO
                        ,'||strColunas||'
                    FROM (SELECT CR.* FROM (SELECT *
           FROM crosstab(''SELECT distinct cdo.chave_veiculo, cdo.chave_categoria codigo, cdo.chave_categoria cat, replace(to_char(sum(cdo.valor),''''FM999999999.00''''),''''.'''','''','''')
                                    FROM controle_despesas_operacionais cdo
                                    where CDO.DATA BETWEEN '''''||parinicio||''''' and '''''||parfinal||'''''
                                     group by cdo.chave_veiculo,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(chave_veiculo integer, cat integer, '||strCategorias||')) CR) CDO
           union all
    SELECT 0 as CHAVE
                        ,''Total'' AS placa
                        ,(SELECT coalesce(replace(to_char(sum(crf.valor_liquido),''FM999999999.00''),''.'','',''),''0,00'') FROM CONTROLE_RECEITAS_FRETES crf WHERE crf.DATA BETWEEN '''||parinicio||''' and '''||parfinal||''') 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
                        ,CDO.CHAVE_VEICULO
                        ,'||strColunas||'
                    FROM (SELECT CR.* FROM (SELECT *
           FROM crosstab(''SELECT distinct 0, cdo.chave_categoria codigo, cdo.chave_categoria cat, replace(to_char(sum(cdo.valor),''''FM999999999.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(chave_veiculo integer, cat integer, '||strCategorias||')) CR) CDO ORDER BY 1';
    ELSE
    	strSQL := 'SELECT row_number() over() as CHAVE
                        ,(Select v.placa from veiculo v where v.codigo = CDO."chave_veiculo") AS placa
                        ,(SELECT coalesce(replace(to_char(sum(crf.valor_liquido),''FM999999999.00''),''.'','',''),''0,00'') FROM CONTROLE_RECEITAS_FRETES crf WHERE crf.chave_veiculo = CDO."chave_veiculo" and crf.DATA BETWEEN '''||parinicio||''' and '''||parfinal||''') AS VALOR_FRETE
                        ,coalesce(replace(to_char(cast((SELECT sum(crf.valor_liquido) FROM CONTROLE_RECEITAS_FRETES crf WHERE crf.chave_veiculo = CDO."chave_veiculo" and crf.DATA BETWEEN '''||parinicio||''' and '''||parfinal||''') - (Select sum(cd.valor) from controle_despesas_operacionais cd where cd.chave_veiculo = CDO."chave_veiculo" and cd.DATA BETWEEN '''||parinicio||''' and '''||parfinal||''') as numeric(15,2)),''FM999999999.00''),''.'','',''),''0,00'') as liquido
                        ,CDO.CHAVE_VEICULO
                        ,'||strColunas||'
                    FROM (SELECT CR.* FROM (SELECT *
           FROM crosstab(''SELECT distinct cdo.chave_veiculo, cdo.chave_categoria codigo, cdo.chave_categoria cat, replace(to_char(sum(cdo.valor),''''FM999999999.00''''),''''.'''','''','''')
                                    FROM controle_despesas_operacionais cdo
                                    where CDO.DATA BETWEEN '''''||parinicio||''''' and '''''||parfinal||'''''
                                    and cdo.chave_veiculo = '||parveiculo||' 
                                     group by cdo.chave_veiculo,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(chave_veiculo integer, cat integer, '||strCategorias||')) CR) CDO
           union all
    SELECT 0 as CHAVE
                        ,''Total'' AS placa
                        ,(SELECT coalesce(replace(to_char(sum(crf.valor_liquido),''FM999999999.00''),''.'','',''),''0,00'') FROM CONTROLE_RECEITAS_FRETES crf WHERE crf.DATA BETWEEN '''||parinicio||''' and '''||parfinal||''' and crf.chave_veiculo = '||parveiculo||') AS VALOR_FRETE
                        ,coalesce(replace(to_char(cast((SELECT coalesce(sum(crf.valor_liquido),0) FROM CONTROLE_RECEITAS_FRETES crf WHERE crf.DATA BETWEEN '''||parinicio||''' and '''||parfinal||''' and crf.chave_veiculo = '||parveiculo||') - (Select sum(cd.valor) from controle_despesas_operacionais cd where cd.DATA BETWEEN '''||parinicio||''' and '''||parfinal||''' and cd.chave_veiculo = '||parveiculo||') as numeric(15,2)),''FM999999999.00''),''.'','',''),''0,00'') as liquido
                        ,CDO.CHAVE_VEICULO
                        ,'||strColunas||'
                    FROM (SELECT CR.* FROM (SELECT *
           FROM crosstab(''SELECT distinct 0, cdo.chave_categoria codigo, cdo.chave_categoria cat, replace(to_char(sum(cdo.valor),''''FM999999999.00''''),''''.'''','''','''')
                                    FROM controle_despesas_operacionais cdo
                                     where CDO.DATA BETWEEN '''''||parinicio||''''' and '''''||parfinal||'''''
                                     and cdo.chave_veiculo = '||parveiculo||' 
                                     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(chave_veiculo integer, cat integer, '||strCategorias||')) CR) CDO ORDER BY 1';
    end if;
   
   return strSQL;

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