if '#1#'='Todas'
select bo.bostamp as stamp,bo.dataobra as dataobra,ano=year(bo.dataobra),
nomedoc=bo.nmdos+' '+CAST(bo.obrano AS VARCHAR (10)),
case when bo.estab>0 then ltrim(rtrim(bo.nome))+' - '+
convert(varchar(10),bo.no)+' - '+
convert(varchar(10),bo.estab)
else bo.nome+ ' - ' +convert(varchar(10),bo.no)
end as nome,
bi.ref,qtt=sum(bi.qtt),
ettdeb=sum((case when bi.ivaincl='1' then (bi.edebito/(iva/100)+1)
else bi.edebito end)*bi.qtt),
case fechada when 0 then 'Aberto'
else 'Fechado'
end estado
from bo (nolock)
inner join bi (nolock) on bi.bostamp=bo.bostamp
inner join st (nolock) on st.ref=bi.ref
where bi.resfor=1 and year(bo.dataobra)='#2#'
and st.ststamp='#STAMP#' and bo.anulada=0
group by bo.bostamp,bo.nmdos,bo.obrano,bo.nome,bo.estab,
bo.no, bo.dataobra,bi.ref, bo.fechada
order by bo.dataobra,bo.nmdos,bo.obrano
if '#1#'='Fechadas'
select bo.bostamp as stamp,bo.dataobra as dataobra, ano=year(bo.dataobra),
nomedoc=bo.nmdos+' '+CAST(bo.obrano AS VARCHAR (10)),
case when bo.estab>0 then ltrim(rtrim(bo.nome))+' - '+
convert(varchar(10),bo.no)+' - '+
convert(varchar(10),bo.estab)
else bo.nome+ ' - ' +convert(varchar(10),bo.no)
end as nome,bi.ref,qtt=sum(bi.qtt),
ettdeb=sum((case when bi.ivaincl='1' then (bi.edebito/(iva/100)+1)
else bi.edebito end)*bi.qtt),
case fechada when 0 then 'Aberto'
else 'Fechado' end estado
from bo (nolock)
inner join bi (nolock) on bi.bostamp=bo.bostamp
inner join st (nolock) on st.ref=bi.ref
where bi.resfor=1 and bo.fechada=1 and year(bo.dataobra)='#2#'
and st.ststamp='#STAMP#' and bo.anulada=0
group by bo.bostamp,bo.nmdos,bo.obrano,bo.nome,bo.estab,
bo.no, bo.dataobra,bi.ref, bo.fechada
order by bo.dataobra,bo.nmdos, bo.obrano
if '#1#'='Abertas'
select bo.bostamp as stamp,bo.dataobra as dataobra,ano=year(bo.dataobra),
nomedoc=bo.nmdos+' '+CAST(bo.obrano AS VARCHAR (10)),
case when bo.estab>0 then ltrim(rtrim(bo.nome))+' - '+
convert(varchar(10),bo.no)+' - '+
convert(varchar(10),bo.estab)
else bo.nome+ ' - ' +convert(varchar(10),bo.no)
end as nome,bi.ref,qtt=sum(bi.qtt),
ettdeb=sum((case when bi.ivaincl='1' then (bi.edebito/(iva/100)+1)
else bi.edebito end)*bi.qtt),
case fechada when 0 then 'Aberto'
else 'Fechado'
end estado
from bo (nolock)
inner join bi (nolock) on bi.bostamp=bo.bostamp
inner join st (nolock) on st.ref=bi.ref
where bi.resfor=1 and bo.fechada<>1
and year(bo.dataobra)='#2#'
and st.ststamp='#STAMP#' and bo.anulada=0
group by bo.bostamp,bo.nmdos,bo.obrano,bo.nome,bo.estab,
bo.no, bo.dataobra,bi.ref, bo.fechada
order by bo.dataobra,bo.nmdos,bo.obrano