select
  inv.*,
  inv.saldo * inv.icms_presumido credito_presumido
from
  (
  select
    st.*,
    cast((select saldo_total from saldo(st.item, 10, 0, '31.12.2018')) as integer) saldo
  from
    (
    select
      i.item,
      i.nome, 
      (select max(ir.data_nota) from itens_recebidos ir where ir.item = i.item and ir.icms_tributacao = 60) ultima_compra,
      cast((select first 1 (ir.icms_valor_nota + ir.diferenca_st + ir.st_base * 0.02) / ir.quantidade from itens_recebidos ir where ir.item = i.item and ir.icms_tributacao = 60) as numeric(13,2)) icms_presumido
    from
      itens i
    where
      i.estoque = 'S'
    ) st
  where
    st.icms_presumido > 0
  ) inv
where
  inv.saldo > 0
order by
  1
