Exemplo
filial
01
01
01
------------------------------------
02
02
02
------------------------------------
03
03
03
SEGUE ABAIXO O CÓDIGO:
create or replace procedure pr_resumo_faturamento_rem
(
p_dt_inicio in date,
p_dt_final in date,
p_cd_filial in varchar2,
p_rg_lista_nd in char,
p_cd_convenio in number,
p_cd_empresa_cobra in number,
p_retorno out long
) is
cursor consulta is
select p.CD_FILIAL,
p.NM_PESSOA_RAZAO_SOCIAL,
p.CD_EMPRESA_COBRA,
p.CD_CONVENIO,
p.TOT_FATURA,
p.VL_ATRAZ_DESC,
p.VL_AVENC_DESC,
p.TOT_NOTA_DEBITO,
nvl(decode (p.CD_CONVENIO,'HAPVIDA',S.TOT_RECEBIDO_CONV + p.TOT_NOTA_DEBITO,S.TOT_RECEBIDO_CONV),0) TOT_RECEBIDO_CONV,
S.TOT_TRIBUTOS,
nvl(S.TOT_GLOSAS,0)TOT_GLOSAS ,
nvl(round (S.PERCT_GLOSAS /p.TOT_FATURA,2),0) PERCT_GLOSAS,
nvl(decode(p.CD_CONVENIO , 'PARTICULAR',0,'SUS',0,p.VL_ATRAZ_DESC + p.VL_AVENC_DESC),0) SLD_A_RECEBER
from (select f.cd_filial,
f.nm_pessoa_razao_social,
re.cd_empresa_cobra,
to_char(decode(to_char(co.fl_tipo),
'2',
'HAPVIDA',
'9',
'PARTICULAR',
decode(to_char(re.cd_convenio),
51,
'SUS',
92,
'SUS',
241,
'SUS',
243,
'SUS',
290,
'SUS',
'OUTROS CONVENIOS'))) cd_convenio,
--
sum(nvl(ob.vl_obrigacao, 0)) +
sum(decode(p_rg_lista_nd,
'S',
decode(co.fl_tipo,
'2',
(fu_nota_debito(nvl(ire.nu_remessa, 0),
nvl(ire.nu_item_remessa, 0)) / 100),
0),
0)) tot_fatura,
--
sum(decode(greatest(to_date(ob.dt_vencimento) - (sysdate - 1), 0),
0,
to_number(ob.vl_saldo_obrigacao),
0)) vl_atraz_desc,
--
sum(decode(greatest(to_date(sysdate - 0) - ob.dt_vencimento, 0),
0,
to_number(ob.vl_saldo_obrigacao),
0)) vl_avenc_desc,
--
sum(decode(p_rg_lista_nd,
'S',
decode(co.fl_tipo,
'2',
(fu_nota_debito(nvl(ire.nu_remessa, 0),
nvl(ire.nu_item_remessa, 0)) / 100),
0),
0)) tot_nota_debito
from vw_filial f,
tb_pessoa pe,
tm_convenio co,
tm_obrigacao ob,
tb_item_remessa_convenio ire,
tm_remessa_convenio re
where re.cd_empresa_cobra = nvl(p_cd_empresa_cobra,re.cd_empresa_cobra)
and re.dt_remessa between
to_date(to_char(p_dt_inicio, 'dd/mm/yyyy'),'DD/MM/YYYY')
and to_date(to_char(p_dt_final, 'dd/mm/yyyy'),'DD/MM/YYYY')
and re.nu_remessa is not null
and re.nu_remessa = ire.nu_remessa
and ((ire.cd_obrigacao = ob.cd_obrigacao) or
(ob.cd_documento_controle = to_char(re.nu_remessa) and
ob.cd_documento_gerador = to_char(re.nu_remessa) and
ob.dt_status >= (trunc(sysdate) - 1000) and
ob.dt_status >=
to_date(to_char(p_dt_inicio, 'dd/mm/yyyy'),'DD/MM/YYYY')))
and re.cd_convenio = co.cd_convenio
and co.cd_pessoa = pe.cd_pessoa
and ob.cd_status + 0 not in (8)
and f.cd_pessoa = re.cd_empresa_cobra
group by f.cd_filial,
f.nm_pessoa_razao_social,
re.cd_empresa_cobra,
to_char(decode(to_char(co.fl_tipo),
'2',
'HAPVIDA',
'9',
'PARTICULAR',
decode(to_char(re.cd_convenio),
51,
'SUS',
92,
'SUS',
241,
'SUS',
243,
'SUS',
290,
'SUS',
'OUTROS CONVENIOS')))
union all
select f.cd_filial,
f.nm_pessoa_razao_social,
f.cd_pessoa,
'PARTICULAR' convenio,
(select sum(o.vl_obrigacao)
from tb_convenio_pagador p,
tm_atendimento a,
tm_obrigacao o
where o.dt_status between
to_date(to_char(p_dt_inicio, 'dd/mm/yyyy'),'DD/MM/YYYY')
and to_date(to_char(p_dt_final, 'dd/mm/yyyy'),'DD/MM/YYYY')
and o.cd_status + 0 not in (8)
and o.cd_filial = f.cd_filial
and o.cd_especie_titulo = 30
and o.cd_tipo_obrigacao = 2
and a.cd_atendimento = fn_limpa_campo(o.cd_documento_gerador, 2)
and p.cd_atendimento = a.cd_atendimento
and p.cd_convenio_pagador = 1) tot_fatura,
--
0 vl_atraz_desc,
--
0 vl_avenc_desc,
--
0 tot_nota_debito
from tb_pessoa pe,
tb_pessoa pf,
vw_filial f,
tb_tipo_transacao a,
tb_operador o,
tm_conta_corrente c,
tm_movimento_transacao muito,
vw_extrato_conta m
where m.dt_transacao between
to_date(to_char(p_dt_inicio, 'dd/mm/yyyy'),'DD/MM/YYYY')
and to_date(to_char(p_dt_final, 'dd/mm/yyyy'),'DD/MM/YYYY')
and m.fl_movimento_conta not in (3)
and m.vl_debito = 0
and muito.cd_movimento_conta = m.cd_movimento
and c.cd_conta_bancaria = m.cd_conta_corrente
and c.cd_tipo_conta_corrente = 1
and c.cd_filial = nvl(p_cd_filial, c.cd_filial)
and o.nm_operador(+) = c.nm_operador
and a.cd_tipo_transacao = m.cd_tipo_transacao
and f.cd_filial = c.cd_filial
and f.cd_filial = nvl(p_cd_filial, f.cd_filial)
and pf.cd_pessoa = f.cd_pessoa
and pe.cd_pessoa = f.cd_pessoa
group by f.cd_filial, f.nm_pessoa_razao_social, f.cd_pessoa, 'PARTICULAR' ) p,
-------consulta secundaria -----
(select CD_EMPRESA_COBRA, CONVENIO, TOT_RECEBIDO_CONV, TOT_TRIBUTOS, TOT_GLOSAS, PERCT_GLOSAS, SLD_A_RECEBER
from(
select re.cd_empresa_cobra,decode(co.fl_tipo,
'2',
'HAPVIDA',
'9',
'PARTICULAR',
decode(re.cd_convenio,
51,
'SUS',
92,
'SUS',
241,
'SUS',
243,
'SUS',
290,
'SUS',
'OUTROS CONVENIOS')) convenio,
--
sum(nvl(mb.vl_lancamento, 0)) tot_recebido_conv,
--
sum(nvl(mb.vl_ir, 0) + nvl(mb.vl_iss, 0) + nvl(mb.vl_cofins, 0) +
nvl(mb.vl_pis, 0) + nvl(mb.vl_csll, 0)) tot_tributos,
--
sum(nvl(mb.vl_abatimento, 0)) tot_glosas,
--
sum(nvl(mb.vl_abatimento, 0) * 100) / 1 perct_glosas,
--
to_number(nvl(null, 0)) +
to_number(nvl(null, 0)) sld_a_receber
--
from tm_movimento_conta mc,
tb_movimento_obrigacao mb,
tb_pessoa pe,
tm_convenio co,
tm_obrigacao ob,
tb_item_remessa_convenio ire,
tm_remessa_convenio re
where '%' != 'PARTICULAR'
and re.cd_empresa_cobra = nvl(p_cd_empresa_cobra,re.cd_empresa_cobra)
and re.dt_remessa between
to_date(to_char(p_dt_inicio, 'dd/mm/yyyy'),'DD/MM/YYYY')
and to_date(to_char(p_dt_final, 'dd/mm/yyyy'),'DD/MM/YYYY')
and re.nu_remessa is not null
and re.nu_remessa = ire.nu_remessa
and ((ire.cd_obrigacao = ob.cd_obrigacao) or
(ob.cd_documento_controle = to_char(re.nu_remessa) and
ob.cd_documento_gerador = to_char(re.nu_remessa) and
ob.dt_status >= (trunc(sysdate) - 365) and
ob.dt_status >= to_date(to_char(p_dt_inicio, 'dd/mm/yyyy'),'DD/MM/YYYY')))
and re.cd_convenio = co.cd_convenio
and co.cd_pessoa = pe.cd_pessoa
and mb.cd_obrigacao = ob.cd_obrigacao
and mc.cd_movimento_conta = mb.cd_movimento_conta
and mc.dt_estorno is null
group by re.cd_empresa_cobra,decode(co.fl_tipo,
'2',
'HAPVIDA',
'9',
'PARTICULAR',
decode(re.cd_convenio,
51,
'SUS',
92,
'SUS',
241,
'SUS',
243,
'SUS',
290,
'SUS',
'OUTROS CONVENIOS'))
union all
select pf.cd_pessoa cd_empresa_cobra,'PARTICULAR' convenio,
--
sum(muito.vl_transacao) tot_recebido_conv,
--
0 tot_tributos,
--
0 tot_glosas,
--
0 perct_glosas,
--
0 sld_a_receber
from tb_pessoa pe,
tb_pessoa pf,
vw_filial f,
tb_tipo_transacao a,
tb_operador o,
tm_conta_corrente c,
tm_movimento_transacao muito,
vw_extrato_conta m
where 'PARTICULAR' = 'PARTICULAR'
and m.dt_transacao between
to_date(to_char(p_dt_inicio, 'dd/mm/yyyy'),'DD/MM/YYYY')
and to_date(to_char(p_dt_final, 'dd/mm/yyyy'),'DD/MM/YYYY')
and m.fl_movimento_conta not in (3)
and m.vl_debito = 0
and muito.cd_movimento_conta = m.cd_movimento
and c.cd_conta_bancaria = m.cd_conta_corrente
and c.cd_tipo_conta_corrente = 1
and c.cd_filial = nvl(p_cd_filial, c.cd_filial)
and o.nm_operador(+) = c.nm_operador
and a.cd_tipo_transacao = m.cd_tipo_transacao
and f.cd_filial = c.cd_filial
and f.cd_filial = nvl(p_cd_filial, f.cd_filial)
and pf.cd_pessoa = f.cd_pessoa
and pe.cd_pessoa = f.cd_pessoa
group by 'PARTICULAR',pf.cd_pessoa))s
where P.CD_EMPRESA_COBRA = S.CD_EMPRESA_COBRA(+)
and P.cd_CONVENIO = S.CONVENIO (+)
and p.cd_filial = nvl(p_cd_filial, p.cd_filial)
and P.CD_EMPRESA_COBRA = nvl(p_cd_empresa_cobra, P.CD_EMPRESA_COBRA);
lv_separador varchar2(1) := ';';
lv_novalinha varchar2(3) := '\n';
begin
p_retorno := 'FILIAL' || lv_separador ||
'RAZAO SOCIAL' || lv_separador ||
'EMPRESA' || lv_separador ||
'CONVENIO' || lv_separador ||
'TOTAL FATURA' || lv_separador ||
'TOTAL RECEBIDO CONVÊNIO' || lv_separador ||
'TOTAL NOTA DEBITO' || lv_separador ||
'TOTAL TRIBUTO' || lv_separador ||
'TOTAL GLOSA' || lv_separador ||
'PORCETAGEM GLOSAS' || lv_separador ||
'SALDO A RECEBER' || lv_novalinha;
for reg in consulta
loop
p_retorno := p_retorno ||
lpad(reg.cd_filial, 3, '0') || lv_separador ||
reg.nm_pessoa_razao_social || lv_separador ||
reg.cd_empresa_cobra || lv_separador ||
reg.cd_convenio || lv_separador ||
reg.tot_fatura || lv_separador ||
reg.tot_recebido_conv || lv_separador ||
reg.tot_nota_debito || lv_separador ||
reg.tot_tributos || lv_separador ||
reg.tot_glosas || lv_separador ||
reg.perct_glosas || lv_separador ||
reg.sld_a_receber || lv_novalinha;
end loop;
exception
when others then
dbms_output.put_line('erro :' || sqlerrm);
end pr_resumo_faturamento_rem;